This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
JsonClassInfo.cs
617 lines (516 loc) · 25.9 KB
/
JsonClassInfo.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Converters;
namespace System.Text.Json
{
[DebuggerDisplay("ClassType.{ClassType}, {Type.Name}")]
internal sealed partial class JsonClassInfo
{
// The length of the property name embedded in the key (in bytes).
// The key is a ulong (8 bytes) containing the first 7 bytes of the property name
// followed by a byte representing the length.
private const int PropertyNameKeyLength = 7;
// The limit to how many property names from the JSON are cached in _propertyRefsSorted before using PropertyCache.
private const int PropertyNameCountCacheThreshold = 64;
// All of the serializable properties on a POCO (except the optional extension property) keyed on property name.
public volatile Dictionary<string, JsonPropertyInfo> PropertyCache;
// All of the serializable properties on a POCO including the optional extension property.
// Used for performance during serialization instead of 'PropertyCache' above.
public volatile JsonPropertyInfo[] PropertyCacheArray;
// Fast cache of properties by first JSON ordering; may not contain all properties. Accessed before PropertyCache.
// Use an array (instead of List<T>) for highest performance.
private volatile PropertyRef[] _propertyRefsSorted;
public delegate object ConstructorDelegate();
public ConstructorDelegate CreateObject { get; private set; }
public ConstructorDelegate CreateConcreteDictionary { get; private set; }
public ClassType ClassType { get; private set; }
public JsonPropertyInfo DataExtensionProperty { get; private set; }
// If enumerable, the JsonClassInfo for the element type.
private JsonClassInfo _elementClassInfo;
/// <summary>
/// Return the JsonClassInfo for the element type, or null if the type is not an enumerable or dictionary.
/// </summary>
/// <remarks>
/// This should not be called during warm-up (initial creation of JsonClassInfos) to avoid recursive behavior
/// which could result in a StackOverflowException.
/// </remarks>
public JsonClassInfo ElementClassInfo
{
get
{
if (_elementClassInfo == null && ElementType != null)
{
Debug.Assert(ClassType == ClassType.Enumerable ||
ClassType == ClassType.Dictionary ||
ClassType == ClassType.IDictionaryConstructible);
_elementClassInfo = Options.GetOrAddClass(ElementType);
}
return _elementClassInfo;
}
}
public Type ElementType { get; set; }
public JsonSerializerOptions Options { get; private set; }
public Type Type { get; private set; }
public void UpdateSortedPropertyCache(ref ReadStackFrame frame)
{
Debug.Assert(frame.PropertyRefCache != null);
// frame.PropertyRefCache is only read\written by a single thread -- the thread performing
// the deserialization for a given object instance.
List<PropertyRef> listToAppend = frame.PropertyRefCache;
// _propertyRefsSorted can be accessed by multiple threads, so replace the reference when
// appending to it. No lock() is necessary.
if (_propertyRefsSorted != null)
{
List<PropertyRef> replacementList = new List<PropertyRef>(_propertyRefsSorted);
Debug.Assert(replacementList.Count <= PropertyNameCountCacheThreshold);
// Verify replacementList will not become too large.
while (replacementList.Count + listToAppend.Count > PropertyNameCountCacheThreshold)
{
// This code path is rare; keep it simple by using RemoveAt() instead of RemoveRange() which requires calculating index\count.
listToAppend.RemoveAt(listToAppend.Count - 1);
}
// Add the new items; duplicates are possible but that is tolerated during property lookup.
replacementList.AddRange(listToAppend);
_propertyRefsSorted = replacementList.ToArray();
}
else
{
_propertyRefsSorted = listToAppend.ToArray();
}
frame.PropertyRefCache = null;
}
public JsonClassInfo(Type type, JsonSerializerOptions options)
{
Type = type;
Options = options;
ClassType = GetClassType(type, options);
CreateObject = options.MemberAccessorStrategy.CreateConstructor(type);
// Ignore properties on enumerable.
switch (ClassType)
{
case ClassType.Object:
{
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
Dictionary<string, JsonPropertyInfo> cache = CreatePropertyCache(properties.Length);
foreach (PropertyInfo propertyInfo in properties)
{
// Ignore indexers
if (propertyInfo.GetIndexParameters().Length > 0)
{
continue;
}
// For now we only support public getters\setters
if (propertyInfo.GetMethod?.IsPublic == true ||
propertyInfo.SetMethod?.IsPublic == true)
{
JsonPropertyInfo jsonPropertyInfo = AddProperty(propertyInfo.PropertyType, propertyInfo, type, options);
Debug.Assert(jsonPropertyInfo != null);
// If the JsonPropertyNameAttribute or naming policy results in collisions, throw an exception.
if (!JsonHelpers.TryAdd(cache, jsonPropertyInfo.NameAsString, jsonPropertyInfo))
{
JsonPropertyInfo other = cache[jsonPropertyInfo.NameAsString];
if (other.ShouldDeserialize == false && other.ShouldSerialize == false)
{
// Overwrite the one just added since it has [JsonIgnore].
cache[jsonPropertyInfo.NameAsString] = jsonPropertyInfo;
}
else if (jsonPropertyInfo.ShouldDeserialize == true || jsonPropertyInfo.ShouldSerialize == true)
{
ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(this, jsonPropertyInfo);
}
// else ignore jsonPropertyInfo since it has [JsonIgnore].
}
}
}
JsonPropertyInfo[] cacheArray;
if (DetermineExtensionDataProperty(cache))
{
// Remove from cache since it is handled independently.
cache.Remove(DataExtensionProperty.NameAsString);
cacheArray = new JsonPropertyInfo[cache.Count + 1];
// Set the last element to the extension property.
cacheArray[cache.Count] = DataExtensionProperty;
}
else
{
cacheArray = new JsonPropertyInfo[cache.Count];
}
// Set fields when finished to avoid concurrency issues.
PropertyCache = cache;
cache.Values.CopyTo(cacheArray, 0);
PropertyCacheArray = cacheArray;
}
break;
case ClassType.Enumerable:
case ClassType.Dictionary:
{
// Add a single property that maps to the class type so we can have policies applied.
AddPolicyProperty(type, options);
Type objectType;
if (IsNativelySupportedCollection(type))
{
// Use the type from the property policy to get any late-bound concrete types (from an interface like IDictionary).
objectType = PolicyProperty.RuntimePropertyType;
}
else
{
// We need to create the declared instance for types implementing natively supported collections.
objectType = PolicyProperty.DeclaredPropertyType;
}
CreateObject = options.MemberAccessorStrategy.CreateConstructor(objectType);
ElementType = GetElementType(type, parentType: null, memberInfo: null, options: options);
}
break;
case ClassType.IDictionaryConstructible:
{
// Add a single property that maps to the class type so we can have policies applied.
AddPolicyProperty(type, options);
ElementType = GetElementType(type, parentType: null, memberInfo: null, options: options);
CreateConcreteDictionary = options.MemberAccessorStrategy.CreateConstructor(
typeof(Dictionary<,>).MakeGenericType(typeof(string), ElementType));
CreateObject = options.MemberAccessorStrategy.CreateConstructor(PolicyProperty.DeclaredPropertyType);
}
break;
case ClassType.Value:
// Add a single property that maps to the class type so we can have policies applied.
AddPolicyProperty(type, options);
break;
case ClassType.Unknown:
// Add a single property that maps to the class type so we can have policies applied.
AddPolicyProperty(type, options);
PropertyCache = new Dictionary<string, JsonPropertyInfo>();
PropertyCacheArray = Array.Empty<JsonPropertyInfo>();
break;
default:
Debug.Fail($"Unexpected class type: {ClassType}");
break;
}
}
private bool DetermineExtensionDataProperty(Dictionary<string, JsonPropertyInfo> cache)
{
JsonPropertyInfo jsonPropertyInfo = GetPropertyWithUniqueAttribute(typeof(JsonExtensionDataAttribute), cache);
if (jsonPropertyInfo != null)
{
Type declaredPropertyType = jsonPropertyInfo.DeclaredPropertyType;
if (!typeof(IDictionary<string, JsonElement>).IsAssignableFrom(declaredPropertyType) &&
!typeof(IDictionary<string, object>).IsAssignableFrom(declaredPropertyType))
{
ThrowHelper.ThrowInvalidOperationException_SerializationDataExtensionPropertyInvalid(this, jsonPropertyInfo);
}
DataExtensionProperty = jsonPropertyInfo;
return true;
}
return false;
}
private JsonPropertyInfo GetPropertyWithUniqueAttribute(Type attributeType, Dictionary<string, JsonPropertyInfo> cache)
{
JsonPropertyInfo property = null;
foreach (JsonPropertyInfo jsonPropertyInfo in cache.Values)
{
Attribute attribute = jsonPropertyInfo.PropertyInfo.GetCustomAttribute(attributeType);
if (attribute != null)
{
if (property != null)
{
ThrowHelper.ThrowInvalidOperationException_SerializationDuplicateTypeAttribute(Type, attributeType);
}
property = jsonPropertyInfo;
}
}
return property;
}
// AggressiveInlining used although a large method it is only called from one location and is on a hot path.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public JsonPropertyInfo GetProperty(ReadOnlySpan<byte> propertyName, ref ReadStackFrame frame)
{
JsonPropertyInfo info = null;
// Keep a local copy of the cache in case it changes by another thread.
PropertyRef[] localPropertyRefsSorted = _propertyRefsSorted;
ulong key = GetKey(propertyName);
// If there is an existing cache, then use it.
if (localPropertyRefsSorted != null)
{
// Start with the current property index, and then go forwards\backwards.
int propertyIndex = frame.PropertyIndex;
int count = localPropertyRefsSorted.Length;
int iForward = Math.Min(propertyIndex, count);
int iBackward = iForward - 1;
while (true)
{
if (iForward < count)
{
PropertyRef propertyRef = localPropertyRefsSorted[iForward];
if (TryIsPropertyRefEqual(propertyRef, propertyName, key, ref info))
{
return info;
}
++iForward;
if (iBackward >= 0)
{
propertyRef = localPropertyRefsSorted[iBackward];
if (TryIsPropertyRefEqual(propertyRef, propertyName, key, ref info))
{
return info;
}
--iBackward;
}
}
else if (iBackward >= 0)
{
PropertyRef propertyRef = localPropertyRefsSorted[iBackward];
if (TryIsPropertyRefEqual(propertyRef, propertyName, key, ref info))
{
return info;
}
--iBackward;
}
else
{
// Property was not found.
break;
}
}
}
// No cached item was found. Try the main list which has all of the properties.
string stringPropertyName = JsonHelpers.Utf8GetString(propertyName);
if (!PropertyCache.TryGetValue(stringPropertyName, out info))
{
info = JsonPropertyInfo.s_missingProperty;
}
Debug.Assert(info != null);
// Three code paths to get here:
// 1) info == s_missingProperty. Property not found.
// 2) key == info.PropertyNameKey. Exact match found.
// 3) key != info.PropertyNameKey. Match found due to case insensitivity.
Debug.Assert(info == JsonPropertyInfo.s_missingProperty || key == info.PropertyNameKey || Options.PropertyNameCaseInsensitive);
// Check if we should add this to the cache.
// Only cache up to a threshold length and then just use the dictionary when an item is not found in the cache.
int cacheCount = 0;
if (localPropertyRefsSorted != null)
{
cacheCount = localPropertyRefsSorted.Length;
}
// Do a quick check for the stable (after warm-up) case.
if (cacheCount < PropertyNameCountCacheThreshold)
{
// Do a slower check for the warm-up case.
if (frame.PropertyRefCache != null)
{
cacheCount += frame.PropertyRefCache.Count;
}
// Check again to append the cache up to the threshold.
if (cacheCount < PropertyNameCountCacheThreshold)
{
if (frame.PropertyRefCache == null)
{
frame.PropertyRefCache = new List<PropertyRef>();
}
PropertyRef propertyRef = new PropertyRef(key, info);
frame.PropertyRefCache.Add(propertyRef);
}
}
return info;
}
private Dictionary<string, JsonPropertyInfo> CreatePropertyCache(int capacity)
{
StringComparer comparer;
if (Options.PropertyNameCaseInsensitive)
{
comparer = StringComparer.OrdinalIgnoreCase;
}
else
{
comparer = StringComparer.Ordinal;
}
return new Dictionary<string, JsonPropertyInfo>(capacity, comparer);
}
public JsonPropertyInfo PolicyProperty { get; private set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryIsPropertyRefEqual(in PropertyRef propertyRef, ReadOnlySpan<byte> propertyName, ulong key, ref JsonPropertyInfo info)
{
if (key == propertyRef.Key)
{
// We compare the whole name, although we could skip the first 7 bytes (but it's not any faster)
if (propertyName.Length <= PropertyNameKeyLength ||
propertyName.SequenceEqual(propertyRef.Info.Name))
{
info = propertyRef.Info;
return true;
}
}
return false;
}
/// <summary>
/// Get a key from the property name.
/// The key consists of the first 7 bytes of the property name and then the length.
/// </summary>
public static ulong GetKey(ReadOnlySpan<byte> propertyName)
{
const int BitsInByte = 8;
ulong key;
int length = propertyName.Length;
if (length > 7)
{
key = MemoryMarshal.Read<ulong>(propertyName);
// Max out the length byte.
// This will cause the comparison logic to always test for equality against the full contents
// when the first 7 bytes are the same.
key |= 0xFF00000000000000;
// It is also possible to include the length up to 0xFF in order to prevent false positives
// when the first 7 bytes match but a different length (up to 0xFF). However the extra logic
// slows key generation in the majority of cases:
// key &= 0x00FFFFFFFFFFFFFF;
// key |= (ulong) 7 << Math.Max(length, 0xFF);
}
else if (length > 3)
{
key = MemoryMarshal.Read<uint>(propertyName);
if (length == 7)
{
key |= (ulong)propertyName[6] << (6 * BitsInByte)
| (ulong)propertyName[5] << (5 * BitsInByte)
| (ulong)propertyName[4] << (4 * BitsInByte)
| (ulong)7 << (7 * BitsInByte);
}
else if (length == 6)
{
key |= (ulong)propertyName[5] << (5 * BitsInByte)
| (ulong)propertyName[4] << (4 * BitsInByte)
| (ulong)6 << (7 * BitsInByte);
}
else if (length == 5)
{
key |= (ulong)propertyName[4] << (4 * BitsInByte)
| (ulong)5 << (7 * BitsInByte);
}
else
{
key |= (ulong)4 << (7 * BitsInByte);
}
}
else if (length > 1)
{
key = MemoryMarshal.Read<ushort>(propertyName);
if (length == 3)
{
key |= (ulong)propertyName[2] << (2 * BitsInByte)
| (ulong)3 << (7 * BitsInByte);
}
else
{
key |= (ulong)2 << (7 * BitsInByte);
}
}
else if (length == 1)
{
key = propertyName[0]
| (ulong)1 << (7 * BitsInByte);
}
else
{
// An empty name is valid.
key = 0;
}
// Verify key contains the embedded bytes as expected.
Debug.Assert(
(length < 1 || propertyName[0] == ((key & ((ulong)0xFF << 8 * 0)) >> 8 * 0)) &&
(length < 2 || propertyName[1] == ((key & ((ulong)0xFF << 8 * 1)) >> 8 * 1)) &&
(length < 3 || propertyName[2] == ((key & ((ulong)0xFF << 8 * 2)) >> 8 * 2)) &&
(length < 4 || propertyName[3] == ((key & ((ulong)0xFF << 8 * 3)) >> 8 * 3)) &&
(length < 5 || propertyName[4] == ((key & ((ulong)0xFF << 8 * 4)) >> 8 * 4)) &&
(length < 6 || propertyName[5] == ((key & ((ulong)0xFF << 8 * 5)) >> 8 * 5)) &&
(length < 7 || propertyName[6] == ((key & ((ulong)0xFF << 8 * 6)) >> 8 * 6)));
return key;
}
// Return the element type of the IEnumerable or return null if not an IEnumerable.
public static Type GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
{
// We want to handle as the implemented collection type, if applicable.
Type implementedType = GetImplementedCollectionType(parentType, propertyType, propertyInfo: null, out _, options);
if (!typeof(IEnumerable).IsAssignableFrom(implementedType))
{
return null;
}
// Check for Array.
Type elementType = implementedType.GetElementType();
if (elementType != null)
{
return elementType;
}
// Check for Dictionary<TKey, TValue> or IEnumerable<T>
if (implementedType.IsGenericType)
{
Type[] args = implementedType.GetGenericArguments();
ClassType classType = GetClassType(implementedType, options);
if ((classType == ClassType.Dictionary || classType == ClassType.IDictionaryConstructible) &&
args.Length >= 2 && // It is >= 2 in case there is a IDictionary<TKey, TValue, TSomeExtension>.
args[0].UnderlyingSystemType == typeof(string))
{
return args[1];
}
if (classType == ClassType.Enumerable && args.Length >= 1) // It is >= 1 in case there is an IEnumerable<T, TSomeExtension>.
{
return args[0];
}
}
if (implementedType.IsAssignableFrom(typeof(IList)) ||
implementedType.IsAssignableFrom(typeof(IDictionary)) ||
IsDeserializedByConstructingWithIList(implementedType) ||
IsDeserializedByConstructingWithIDictionary(implementedType))
{
return typeof(object);
}
throw ThrowHelper.GetNotSupportedException_SerializationNotSupportedCollection(propertyType, parentType, memberInfo);
}
public static ClassType GetClassType(Type type, JsonSerializerOptions options)
{
Debug.Assert(type != null);
// We want to handle as the implemented collection type, if applicable.
Type implementedType = GetImplementedCollectionType(typeof(object), type, propertyInfo: null, out _, options);
if (implementedType.IsGenericType && implementedType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
implementedType = Nullable.GetUnderlyingType(implementedType);
}
if (implementedType == typeof(object))
{
return ClassType.Unknown;
}
if (options.HasConverter(implementedType))
{
return ClassType.Value;
}
if (DefaultImmutableDictionaryConverter.IsImmutableDictionary(implementedType) ||
IsDeserializedByConstructingWithIDictionary(implementedType))
{
return ClassType.IDictionaryConstructible;
}
if (typeof(IDictionary).IsAssignableFrom(implementedType) || IsDictionaryClassType(implementedType))
{
// Special case for immutable dictionaries
if (type != implementedType && !IsNativelySupportedCollection(type))
{
return ClassType.IDictionaryConstructible;
}
return ClassType.Dictionary;
}
if (typeof(IEnumerable).IsAssignableFrom(implementedType))
{
return ClassType.Enumerable;
}
return ClassType.Object;
}
public static bool IsDictionaryClassType(Type type)
{
return (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary<,>) ||
type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>)));
}
}
}