-
Notifications
You must be signed in to change notification settings - Fork 45
/
JsonElementAttributesTable.cs
572 lines (500 loc) · 19.9 KB
/
JsonElementAttributesTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
namespace NetTopologySuite.Features
{
/// <summary>
/// An implementation of <see cref="IAttributesTable"/> backed by a <see cref="JsonElement"/>
/// whose <see cref="JsonElement.ValueKind"/> is <see cref="JsonValueKind.Object"/>.
/// </summary>
/// <remarks>
/// JSON <see cref="JsonValueKind.Number"/>-valued properties are boxed as <see cref="decimal"/>
/// values unconditionally, throwing an error if the number is not convertible.
/// <para/>
/// JSON <see cref="JsonValueKind.Null"/>-valued properties are considered to be of type
/// <see cref="object"/>, for the purposes of <see cref="IAttributesTable.GetType"/> only.
/// <para/>
/// JSON <see cref="JsonValueKind.Object"/>-valued properties are wrapped in their own
/// <see cref="JsonElementAttributesTable"/> objects.
/// </remarks>
public sealed class JsonElementAttributesTable : IPartiallyDeserializedAttributesTable
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonElementAttributesTable"/> class.
/// </summary>
/// <param name="rootElement">
/// The value for <see cref="RootElement"/>.
/// </param>
/// <remarks>
/// <paramref name="rootElement"/> will be <see cref="JsonElement.Clone">cloned</see> to
/// ensure that it is safe for this object to outlive the <see cref="JsonDocument"/> that
/// the given element is attached to, if any.
/// </remarks>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="rootElement"/>'s <see cref="JsonElement.ValueKind"/> is not
/// <see cref="JsonValueKind.Object"/>.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// Thrown when <paramref name="rootElement"/> is attached to a <see cref="JsonDocument"/>
/// parent that has already been disposed.
/// </exception>
public JsonElementAttributesTable(JsonElement rootElement)
{
if (rootElement.ValueKind != JsonValueKind.Object)
{
throw new ArgumentException($"{nameof(rootElement.ValueKind)} must be {nameof(JsonValueKind.Object)}, not {rootElement.ValueKind}", nameof(rootElement));
}
RootElement = rootElement.Clone();
}
/// <summary>
/// Gets the <see cref="JsonElement"/> object that this instance adapts to fit the
/// <see cref="IAttributesTable"/> interface.
/// </summary>
/// <remarks>
/// <see cref="JsonElement.ValueKind"/> will be <see cref="JsonValueKind.Object"/>.
/// </remarks>
public JsonElement RootElement { get; }
/// <inheritdoc />
public object this[string attributeName]
{
get
{
return RootElement.TryGetProperty(attributeName, out var prop)
? ConvertValue(prop)
: throw new ArgumentException($"Attribute {attributeName} does not exist!", nameof(attributeName));
}
set
{
ThrowNotSupportedExceptionForReadOnlyTable();
}
}
/// <inheritdoc />
public int Count => RootElement.EnumerateObject().Count();
/// <inheritdoc />
public bool Exists(string attributeName)
{
return RootElement.TryGetProperty(attributeName, out _);
}
/// <inheritdoc />
public object GetOptionalValue(string attributeName)
{
return RootElement.TryGetProperty(attributeName, out var prop)
? ConvertValue(prop)
: null;
}
/// <inheritdoc />
public Type GetType(string attributeName)
{
if (!RootElement.TryGetProperty(attributeName, out var prop))
{
throw new ArgumentException($"Attribute {attributeName} does not exist!", nameof(attributeName));
}
return ConvertValue(prop)?.GetType() ?? typeof(object);
}
/// <inheritdoc />
public string[] GetNames()
{
return RootElement.EnumerateObject()
.Select(prop => prop.Name)
.ToArray();
}
/// <inheritdoc />
public object[] GetValues()
{
return RootElement.EnumerateObject()
.Select(prop => GetOptionalValue(prop.Name))
.ToArray();
}
/// <inheritdoc />
public bool TryDeserializeJsonObject<T>(JsonSerializerOptions options, out T deserialized)
{
return TryDeserializeElement(this.RootElement, options, out deserialized);
}
/// <inheritdoc />
public bool TryGetJsonObjectPropertyValue<T>(string propertyName, JsonSerializerOptions options, out T deserialized)
{
if (!this.RootElement.TryGetProperty(propertyName, out var elementToTransform))
{
deserialized = default;
return false;
}
return TryDeserializeElement(elementToTransform, options, out deserialized);
}
void IAttributesTable.Add(string attributeName, object value)
{
ThrowNotSupportedExceptionForReadOnlyTable();
}
void IAttributesTable.DeleteAttribute(string attributeName)
{
ThrowNotSupportedExceptionForReadOnlyTable();
}
private static void ThrowNotSupportedExceptionForReadOnlyTable()
{
throw new NotSupportedException("Modifying this attribute table is not supported.");
}
private static bool TryDeserializeElement<T>(JsonElement elementToTransform, JsonSerializerOptions options, out T deserialized)
{
deserialized = default;
// try the types that the framework has builtins for. all but one of these top-level
// branches should go away at JIT time.
if (typeof(T) == typeof(byte))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref byte u8 = ref Unsafe.As<T, byte>(ref deserialized);
return elementToTransform.TryGetByte(out u8);
}
else if (typeof(T) == typeof(byte?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetByte(out byte u8)))
{
return false;
}
Unsafe.As<T, byte?>(ref deserialized) = u8;
return true;
}
else if (typeof(T) == typeof(sbyte))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref sbyte s8 = ref Unsafe.As<T, sbyte>(ref deserialized);
return elementToTransform.TryGetSByte(out s8);
}
else if (typeof(T) == typeof(sbyte?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetSByte(out sbyte s8)))
{
return false;
}
Unsafe.As<T, sbyte?>(ref deserialized) = s8;
return true;
}
else if (typeof(T) == typeof(short))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref short s16 = ref Unsafe.As<T, short>(ref deserialized);
return elementToTransform.TryGetInt16(out s16);
}
else if (typeof(T) == typeof(short?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetInt16(out short s16)))
{
return false;
}
Unsafe.As<T, short?>(ref deserialized) = s16;
return true;
}
else if (typeof(T) == typeof(ushort))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref ushort u16 = ref Unsafe.As<T, ushort>(ref deserialized);
return elementToTransform.TryGetUInt16(out u16);
}
else if (typeof(T) == typeof(ushort?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetUInt16(out ushort u16)))
{
return false;
}
Unsafe.As<T, ushort?>(ref deserialized) = u16;
return true;
}
else if (typeof(T) == typeof(int))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref int s32 = ref Unsafe.As<T, int>(ref deserialized);
return elementToTransform.TryGetInt32(out s32);
}
else if (typeof(T) == typeof(int?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetInt32(out int s32)))
{
return false;
}
Unsafe.As<T, int?>(ref deserialized) = s32;
return true;
}
else if (typeof(T) == typeof(uint))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref uint u32 = ref Unsafe.As<T, uint>(ref deserialized);
return elementToTransform.TryGetUInt32(out u32);
}
else if (typeof(T) == typeof(uint?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetUInt32(out uint u32)))
{
return false;
}
Unsafe.As<T, uint?>(ref deserialized) = u32;
return true;
}
else if (typeof(T) == typeof(long))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref long s64 = ref Unsafe.As<T, long>(ref deserialized);
return elementToTransform.TryGetInt64(out s64);
}
else if (typeof(T) == typeof(long?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetInt64(out long s64)))
{
return false;
}
Unsafe.As<T, long?>(ref deserialized) = s64;
return true;
}
else if (typeof(T) == typeof(ulong))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref ulong u64 = ref Unsafe.As<T, ulong>(ref deserialized);
return elementToTransform.TryGetUInt64(out u64);
}
else if (typeof(T) == typeof(ulong?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetUInt64(out ulong u64)))
{
return false;
}
Unsafe.As<T, ulong?>(ref deserialized) = u64;
return true;
}
else if (typeof(T) == typeof(float))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref float f32 = ref Unsafe.As<T, float>(ref deserialized);
return elementToTransform.TryGetSingle(out f32);
}
else if (typeof(T) == typeof(float?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetSingle(out float f32)))
{
return false;
}
Unsafe.As<T, float?>(ref deserialized) = f32;
return true;
}
else if (typeof(T) == typeof(double))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref double f64 = ref Unsafe.As<T, double>(ref deserialized);
return elementToTransform.TryGetDouble(out f64);
}
else if (typeof(T) == typeof(double?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetDouble(out double f64)))
{
return false;
}
Unsafe.As<T, double?>(ref deserialized) = f64;
return true;
}
else if (typeof(T) == typeof(decimal))
{
if (elementToTransform.ValueKind != JsonValueKind.Number)
{
return false;
}
ref decimal d128 = ref Unsafe.As<T, decimal>(ref deserialized);
return elementToTransform.TryGetDecimal(out d128);
}
else if (typeof(T) == typeof(decimal?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.Number && elementToTransform.TryGetDecimal(out decimal d128)))
{
return false;
}
Unsafe.As<T, decimal?>(ref deserialized) = d128;
return true;
}
else if (typeof(T) == typeof(DateTime))
{
if (elementToTransform.ValueKind != JsonValueKind.String)
{
return false;
}
ref var dt = ref Unsafe.As<T, DateTime>(ref deserialized);
return elementToTransform.TryGetDateTime(out dt);
}
else if (typeof(T) == typeof(DateTime?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.String && elementToTransform.TryGetDateTime(out var dt)))
{
return false;
}
Unsafe.As<T, DateTime?>(ref deserialized) = dt;
return true;
}
else if (typeof(T) == typeof(DateTimeOffset))
{
if (elementToTransform.ValueKind != JsonValueKind.String)
{
return false;
}
ref var dto = ref Unsafe.As<T, DateTimeOffset>(ref deserialized);
return elementToTransform.TryGetDateTimeOffset(out dto);
}
else if (typeof(T) == typeof(DateTimeOffset?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.String && elementToTransform.TryGetDateTimeOffset(out var dto)))
{
return false;
}
Unsafe.As<T, DateTimeOffset?>(ref deserialized) = dto;
return true;
}
else if (typeof(T) == typeof(Guid))
{
if (elementToTransform.ValueKind != JsonValueKind.String)
{
return false;
}
ref var guid = ref Unsafe.As<T, Guid>(ref deserialized);
return elementToTransform.TryGetGuid(out guid);
}
else if (typeof(T) == typeof(Guid?))
{
if (elementToTransform.ValueKind == JsonValueKind.Null)
{
return true;
}
if (!(elementToTransform.ValueKind == JsonValueKind.String && elementToTransform.TryGetGuid(out var guid)))
{
return false;
}
Unsafe.As<T, Guid?>(ref deserialized) = guid;
return true;
}
else if (typeof(T) == typeof(string))
{
if (elementToTransform.ValueKind != JsonValueKind.String && elementToTransform.ValueKind != JsonValueKind.Null)
{
return false;
}
ref string str = ref Unsafe.As<T, string>(ref deserialized);
str = elementToTransform.GetString();
return true;
}
else
{
try
{
deserialized = JsonSerializer.Deserialize<T>(elementToTransform, options);
return true;
}
catch (JsonException)
{
return false;
}
}
}
internal static object ConvertValue(JsonElement prop)
{
switch (prop.ValueKind)
{
case JsonValueKind.Undefined:
case JsonValueKind.Null:
return null;
case JsonValueKind.False:
return false;
case JsonValueKind.True:
return true;
case JsonValueKind.String:
return prop.GetString();
case JsonValueKind.Object:
return new JsonElementAttributesTable(prop);
case JsonValueKind.Array:
return prop.EnumerateArray()
.Select(ConvertValue)
.ToArray();
case JsonValueKind.Number when prop.TryGetDecimal(out decimal d):
return d;
case JsonValueKind.Number:
throw new NotSupportedException("Number value cannot be boxed as a decimal: " + prop.GetRawText());
default:
throw new NotSupportedException("Unrecognized JsonValueKind: " + prop.ValueKind);
}
}
}
}