6
6
using System . Linq ;
7
7
using System . Reflection ;
8
8
using System . Text . Json ;
9
+ using Azure . Core . Json ;
9
10
10
11
namespace Azure . Core . Dynamic
11
12
{
@@ -21,66 +22,181 @@ public partial class DynamicData
21
22
/// Converts the value to a <see cref="bool"/>.
22
23
/// </summary>
23
24
/// <param name="value">The value to convert.</param>
24
- public static implicit operator bool ( DynamicData value ) => value . _element . GetBoolean ( ) ;
25
+ public static implicit operator bool ( DynamicData value )
26
+ {
27
+ try
28
+ {
29
+ return value . _element . GetBoolean ( ) ;
30
+ }
31
+ catch ( InvalidOperationException e )
32
+ {
33
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( bool ) , value . _element ) , e ) ;
34
+ }
35
+ }
25
36
26
37
/// <summary>
27
38
/// Converts the value to a <see cref="int"/>.
28
39
/// </summary>
29
40
/// <param name="value">The value to convert.</param>
30
- public static implicit operator int ( DynamicData value ) => value . _element . GetInt32 ( ) ;
41
+ public static implicit operator int ( DynamicData value )
42
+ {
43
+ try
44
+ {
45
+ return value . _element . GetInt32 ( ) ;
46
+ }
47
+ catch ( InvalidOperationException e )
48
+ {
49
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( int ) , value . _element ) , e ) ;
50
+ }
51
+ }
31
52
32
53
/// <summary>
33
54
/// Converts the value to a <see cref="long"/>.
34
55
/// </summary>
35
56
/// <param name="value">The value to convert.</param>
36
- public static implicit operator long ( DynamicData value ) => value . _element . GetInt64 ( ) ;
57
+ public static implicit operator long ( DynamicData value )
58
+ {
59
+ try
60
+ {
61
+ return value . _element . GetInt64 ( ) ;
62
+ }
63
+ catch ( InvalidOperationException e )
64
+ {
65
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( long ) , value . _element ) , e ) ;
66
+ }
67
+ }
37
68
38
69
/// <summary>
39
70
/// Converts the value to a <see cref="string"/>.
40
71
/// </summary>
41
72
/// <param name="value">The value to convert.</param>
42
- public static implicit operator string ? ( DynamicData value ) => value . _element . GetString ( ) ;
73
+ public static implicit operator string ? ( DynamicData value )
74
+ {
75
+ try
76
+ {
77
+ return value . _element . GetString ( ) ;
78
+ }
79
+ catch ( InvalidOperationException e )
80
+ {
81
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( string ) , value . _element ) , e ) ;
82
+ }
83
+ }
43
84
44
85
/// <summary>
45
86
/// Converts the value to a <see cref="float"/>.
46
87
/// </summary>
47
88
/// <param name="value">The value to convert.</param>
48
- public static implicit operator float ( DynamicData value ) => value . _element . GetSingle ( ) ;
89
+ public static implicit operator float ( DynamicData value )
90
+ {
91
+ try
92
+ {
93
+ return value . _element . GetSingle ( ) ;
94
+ }
95
+ catch ( InvalidOperationException e )
96
+ {
97
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( float ) , value . _element ) , e ) ;
98
+ }
99
+ }
49
100
50
101
/// <summary>
51
102
/// Converts the value to a <see cref="double"/>.
52
103
/// </summary>
53
104
/// <param name="value">The value to convert.</param>
54
- public static implicit operator double ( DynamicData value ) => value . _element . GetDouble ( ) ;
105
+ public static implicit operator double ( DynamicData value )
106
+ {
107
+ try
108
+ {
109
+ return value . _element . GetDouble ( ) ;
110
+ }
111
+ catch ( InvalidOperationException e )
112
+ {
113
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( double ) , value . _element ) , e ) ;
114
+ }
115
+ }
55
116
56
117
/// <summary>
57
118
/// Converts the value to a <see cref="bool"/> or null.
58
119
/// </summary>
59
120
/// <param name="value">The value to convert.</param>
60
- public static implicit operator bool ? ( DynamicData value ) => value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetBoolean ( ) ;
121
+ public static implicit operator bool ? ( DynamicData value )
122
+ {
123
+ try
124
+ {
125
+ return value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetBoolean ( ) ;
126
+ }
127
+ catch ( InvalidOperationException e )
128
+ {
129
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( bool ? ) , value . _element ) , e ) ;
130
+ }
131
+ }
61
132
62
133
/// <summary>
63
134
/// Converts the value to a <see cref="int"/> or null.
64
135
/// </summary>
65
136
/// <param name="value">The value to convert.</param>
66
- public static implicit operator int ? ( DynamicData value ) => value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetInt32 ( ) ;
137
+ public static implicit operator int ? ( DynamicData value )
138
+ {
139
+ try
140
+ {
141
+ return value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetInt32 ( ) ;
142
+ }
143
+ catch ( InvalidOperationException e )
144
+ {
145
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( int ? ) , value . _element ) , e ) ;
146
+ }
147
+ }
67
148
68
149
/// <summary>
69
150
/// Converts the value to a <see cref="long"/> or null.
70
151
/// </summary>
71
152
/// <param name="value">The value to convert.</param>
72
- public static implicit operator long ? ( DynamicData value ) => value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetInt64 ( ) ;
153
+ public static implicit operator long ? ( DynamicData value )
154
+ {
155
+ try
156
+ {
157
+ return value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetInt64 ( ) ;
158
+ }
159
+ catch ( InvalidOperationException e )
160
+ {
161
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( long ? ) , value . _element ) , e ) ;
162
+ }
163
+ }
73
164
74
165
/// <summary>
75
166
/// Converts the value to a <see cref="float"/> or null.
76
167
/// </summary>
77
168
/// <param name="value">The value to convert.</param>
78
- public static implicit operator float ? ( DynamicData value ) => value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetSingle ( ) ;
169
+ public static implicit operator float ? ( DynamicData value )
170
+ {
171
+ try
172
+ {
173
+ return value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetSingle ( ) ;
174
+ }
175
+ catch ( InvalidOperationException e )
176
+ {
177
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( float ? ) , value . _element ) , e ) ;
178
+ }
179
+ }
79
180
80
181
/// <summary>
81
182
/// Converts the value to a <see cref="double"/> or null.
82
183
/// </summary>
83
184
/// <param name="value">The value to convert.</param>
84
- public static implicit operator double ? ( DynamicData value ) => value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetDouble ( ) ;
185
+ public static implicit operator double ? ( DynamicData value )
186
+ {
187
+ try
188
+ {
189
+ return value . _element . ValueKind == JsonValueKind . Null ? null : value . _element . GetDouble ( ) ;
190
+ }
191
+ catch ( InvalidOperationException e )
192
+ {
193
+ throw new InvalidCastException ( GetInvalidCastExceptionText ( typeof ( double ? ) , value . _element ) , e ) ;
194
+ }
195
+ }
196
+
197
+ private static string GetInvalidCastExceptionText ( Type target , MutableJsonElement element )
198
+ {
199
+ return $ "Unable to cast element to '{ target } '. Element has kind '{ element . ValueKind } '.";
200
+ }
85
201
}
86
202
}
0 commit comments