@@ -24,6 +24,26 @@ public static class JsonRecordExtensions
2424 return JsonSerializer . Deserialize < T > ( json , options ) ;
2525 }
2626
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 ) )
37+ throw new DataException ( $ "JSON column at ordinal { ordinal } is null but a value of type '{ typeof ( T ) } ' is required.") ;
38+
39+ var json = dataRecord . GetString ( ordinal ) ;
40+ var value = JsonSerializer . Deserialize < T > ( json , options ) ;
41+
42+ return value is null
43+ ? throw new DataException ( $ "JSON column at ordinal { ordinal } deserialized to null but a value of type '{ typeof ( T ) } ' is required.")
44+ : value ;
45+ }
46+
2747 /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
2848 /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
2949 /// <param name="dataRecord">The data record.</param>
@@ -39,6 +59,26 @@ public static class JsonRecordExtensions
3959 return JsonSerializer . Deserialize ( json , jsonTypeInfo ) ;
4060 }
4161
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 ) )
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 ) ;
75+ var value = JsonSerializer . Deserialize ( json , jsonTypeInfo ) ;
76+
77+ return value is null
78+ ? throw new DataException ( $ "JSON column at ordinal { ordinal } deserialized to null but a value of type '{ typeof ( T ) } ' is required.")
79+ : value ;
80+ }
81+
4282 /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
4383 /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
4484 /// <param name="dataRecord">The data record.</param>
@@ -51,6 +91,19 @@ public static class JsonRecordExtensions
5191 return dataRecord . GetFromJson < T > ( ordinal , options ) ;
5292 }
5393
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 ) ;
104+ return dataRecord . GetRequiredFromJson < T > ( ordinal , options ) ;
105+ }
106+
54107 /// <summary>Deserializes the JSON value of the specified column to <typeparamref name="T"/>.</summary>
55108 /// <typeparam name="T">The type to deserialize the JSON value into.</typeparam>
56109 /// <param name="dataRecord">The data record.</param>
@@ -62,4 +115,17 @@ public static class JsonRecordExtensions
62115 int ordinal = dataRecord . GetOrdinal ( name ) ;
63116 return dataRecord . GetFromJson < T > ( ordinal , jsonTypeInfo ) ;
64117 }
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 ) ;
129+ return dataRecord . GetRequiredFromJson < T > ( ordinal , jsonTypeInfo ) ;
130+ }
65131}
0 commit comments