• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 26618624650

29 May 2026 04:45AM UTC coverage: 55.778% (+0.03%) from 55.746%
26618624650

push

github

pwelter34
Handle nullable props and required JSON readers

1390 of 3261 branches covered (42.62%)

Branch coverage included in aggregate %.

47 of 54 new or added lines in 4 files covered. (87.04%)

1 existing line in 1 file now uncovered.

4455 of 7218 relevant lines covered (61.72%)

310.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.29
/src/FluentCommand/Extensions/JsonRecordExtensions.cs
1
using System.Data;
2
using System.Text.Json;
3
using System.Text.Json.Serialization.Metadata;
4

5
namespace FluentCommand.Extensions;
6

7
/// <summary>
8
/// Extension methods for deserializing JSON columns from an <see cref="IDataRecord"/>.
9
/// </summary>
10
public static class JsonRecordExtensions
11
{
12
    /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
13
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
14
    /// <param name="dataRecord">The data record.</param>
15
    /// <param name="ordinal">The zero-based column ordinal.</param>
16
    /// <param name="options">Options to control the behavior during parsing.</param>
17
    /// <returns>The deserialized value, or <see langword="default"/> if the column is <see langword="null"/>.</returns>
18
    public static T? GetFromJson<T>(this IDataRecord dataRecord, int ordinal, JsonSerializerOptions? options = null)
19
    {
20
        if (dataRecord.IsDBNull(ordinal))
3!
21
            return default;
×
22

23
        var json = dataRecord.GetString(ordinal);
3✔
24
        return JsonSerializer.Deserialize<T>(json, options);
3✔
25
    }
26

27
    /// <summary>Deserializes the JSON value of the specified column to a required <typeparamref name="T"/> value.</summary>
28
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
29
    /// <param name="dataRecord">The data record.</param>
30
    /// <param name="ordinal">The zero-based column ordinal.</param>
31
    /// <param name="options">Options to control the behavior during parsing.</param>
32
    /// <returns>The deserialized value.</returns>
33
    /// <exception cref="DataException">The column is <see langword="null"/> or deserializes to <see langword="null"/>.</exception>
34
    public static T GetRequiredFromJson<T>(this IDataRecord dataRecord, int ordinal, JsonSerializerOptions? options = null)
35
    {
36
        if (dataRecord.IsDBNull(ordinal))
3✔
37
            throw new DataException($"JSON column at ordinal {ordinal} is null but a value of type '{typeof(T)}' is required.");
1✔
38

39
        var json = dataRecord.GetString(ordinal);
2✔
40
        var value = JsonSerializer.Deserialize<T>(json, options);
2✔
41

42
        return value is null
2✔
43
            ? throw new DataException($"JSON column at ordinal {ordinal} deserialized to null but a value of type '{typeof(T)}' is required.")
2✔
44
            : value;
2✔
45
    }
46

47
    /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
48
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
49
    /// <param name="dataRecord">The data record.</param>
50
    /// <param name="ordinal">The zero-based column ordinal.</param>
51
    /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
52
    /// <returns>The deserialized value, or <see langword="default"/> if the column is <see langword="null"/>.</returns>
53
    public static T? GetFromJson<T>(this IDataRecord dataRecord, int ordinal, JsonTypeInfo<T> jsonTypeInfo)
54
    {
55
        if (dataRecord.IsDBNull(ordinal))
×
56
            return default;
×
57

58
        var json = dataRecord.GetString(ordinal);
×
59
        return JsonSerializer.Deserialize(json, jsonTypeInfo);
×
60
    }
61

62
    /// <summary>Deserializes the JSON value of the specified column to a required <typeparamref name="T"/> value.</summary>
63
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
64
    /// <param name="dataRecord">The data record.</param>
65
    /// <param name="ordinal">The zero-based column ordinal.</param>
66
    /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
67
    /// <returns>The deserialized value.</returns>
68
    /// <exception cref="DataException">The column is <see langword="null"/> or deserializes to <see langword="null"/>.</exception>
69
    public static T GetRequiredFromJson<T>(this IDataRecord dataRecord, int ordinal, JsonTypeInfo<T> jsonTypeInfo)
70
    {
71
        if (dataRecord.IsDBNull(ordinal))
1!
NEW
72
            throw new DataException($"JSON column at ordinal {ordinal} is null but a value of type '{typeof(T)}' is required.");
×
73

74
        var json = dataRecord.GetString(ordinal);
1✔
75
        var value = JsonSerializer.Deserialize(json, jsonTypeInfo);
1✔
76

77
        return value is null
1!
78
            ? throw new DataException($"JSON column at ordinal {ordinal} deserialized to null but a value of type '{typeof(T)}' is required.")
1✔
79
            : value;
1✔
80
    }
81

82
    /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
83
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
84
    /// <param name="dataRecord">The data record.</param>
85
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
86
    /// <param name="options">Options to control the behavior during parsing.</param>
87
    /// <returns>The deserialized value, or <see langword="default"/> if the column is <see langword="null"/>.</returns>
88
    public static T? GetFromJson<T>(this IDataRecord dataRecord, string name, JsonSerializerOptions? options = null)
89
    {
90
        int ordinal = dataRecord.GetOrdinal(name);
×
91
        return dataRecord.GetFromJson<T>(ordinal, options);
×
92
    }
93

94
    /// <summary>Deserializes the JSON value of the specified column to a required <typeparamref name="T"/> value.</summary>
95
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
96
    /// <param name="dataRecord">The data record.</param>
97
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
98
    /// <param name="options">Options to control the behavior during parsing.</param>
99
    /// <returns>The deserialized value.</returns>
100
    /// <exception cref="DataException">The column is <see langword="null"/> or deserializes to <see langword="null"/>.</exception>
101
    public static T GetRequiredFromJson<T>(this IDataRecord dataRecord, string name, JsonSerializerOptions? options = null)
102
    {
103
        int ordinal = dataRecord.GetOrdinal(name);
3✔
104
        return dataRecord.GetRequiredFromJson<T>(ordinal, options);
3✔
105
    }
106

107
    /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
108
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
109
    /// <param name="dataRecord">The data record.</param>
110
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
111
    /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
112
    /// <returns>The deserialized value, or <see langword="default"/> if the column is <see langword="null"/>.</returns>
113
    public static T? GetFromJson<T>(this IDataRecord dataRecord, string name, JsonTypeInfo<T> jsonTypeInfo)
114
    {
115
        int ordinal = dataRecord.GetOrdinal(name);
×
116
        return dataRecord.GetFromJson<T>(ordinal, jsonTypeInfo);
×
117
    }
118

119
    /// <summary>Deserializes the JSON value of the specified column to a required <typeparamref name="T"/> value.</summary>
120
    /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
121
    /// <param name="dataRecord">The data record.</param>
122
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
123
    /// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
124
    /// <returns>The deserialized value.</returns>
125
    /// <exception cref="DataException">The column is <see langword="null"/> or deserializes to <see langword="null"/>.</exception>
126
    public static T GetRequiredFromJson<T>(this IDataRecord dataRecord, string name, JsonTypeInfo<T> jsonTypeInfo)
127
    {
128
        int ordinal = dataRecord.GetOrdinal(name);
1✔
129
        return dataRecord.GetRequiredFromJson<T>(ordinal, jsonTypeInfo);
1✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc