Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonPropertyInfo.cs
Go to the documentation of this file.
6
8
9[DebuggerDisplay("MemberInfo={MemberInfo}")]
11public abstract class JsonPropertyInfo
12{
14
16
18
19 internal abstract JsonConverter ConverterBase { get; set; }
20
21 internal Type DeclaredPropertyType { get; set; }
22
23 internal bool HasGetter { get; set; }
24
25 internal bool HasSetter { get; set; }
26
27 internal bool IgnoreDefaultValuesOnRead { get; private set; }
28
29 internal bool IgnoreDefaultValuesOnWrite { get; private set; }
30
31 internal bool IsForTypeInfo { get; set; }
32
33 internal string NameAsString { get; set; }
34
35 internal byte[] NameAsUtf8Bytes { get; set; }
36
37 internal byte[] EscapedNameSection { get; set; }
38
39 internal JsonSerializerOptions Options { get; set; }
40
41 internal int Order { get; set; }
42
43 internal Type DeclaringType { get; set; }
44
45 internal MemberInfo? MemberInfo { get; private set; }
46
48 {
49 get
50 {
51 if (_runtimeTypeInfo == null)
52 {
54 }
55 return _runtimeTypeInfo;
56 }
57 set
58 {
60 }
61 }
62
63 internal Type? RuntimePropertyType { get; set; }
64
65 internal bool ShouldSerialize { get; set; }
66
67 internal bool ShouldDeserialize { get; set; }
68
69 internal bool IsIgnored { get; set; }
70
71 internal bool SrcGen_HasJsonInclude { get; set; }
72
73 internal bool SrcGen_IsExtensionData { get; set; }
74
75 internal bool SrcGen_IsPublic { get; set; }
76
77 internal JsonNumberHandling? NumberHandling { get; set; }
78
79 internal bool PropertyTypeCanBeNull { get; set; }
80
81 internal JsonIgnoreCondition? IgnoreCondition { get; set; }
82
83 internal MemberTypes MemberType { get; set; }
84
85 internal string? ClrName { get; set; }
86
87 internal bool IsVirtual { get; set; }
88
89 internal abstract object? DefaultValue { get; }
90
92 {
93 }
94
101
113
131
161
163 {
164 if ((ConverterStrategy & (ConverterStrategy)24) == 0)
165 {
167 ShouldSerialize = HasGetter && (HasSetter || flag);
169 }
170 else if (HasGetter)
171 {
172 ShouldSerialize = true;
173 if (HasSetter)
174 {
175 ShouldDeserialize = true;
176 }
177 }
178 }
179
181 {
182 if (ignoreCondition.HasValue)
183 {
184 if (ignoreCondition == JsonIgnoreCondition.WhenWritingDefault)
185 {
187 }
188 else if (ignoreCondition == JsonIgnoreCondition.WhenWritingNull)
189 {
191 {
193 }
194 else
195 {
197 }
198 }
199 }
200 else if (Options.IgnoreNullValues)
201 {
203 {
206 }
207 }
208 else if (Options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull)
209 {
211 {
213 }
214 }
215 else if (Options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingDefault)
216 {
218 }
219 }
220
236
253
255 {
257 {
258 return true;
259 }
262 if (!(type == typeof(byte)) && !(type == typeof(decimal)) && !(type == typeof(double)) && !(type == typeof(short)) && !(type == typeof(int)) && !(type == typeof(long)) && !(type == typeof(sbyte)) && !(type == typeof(float)) && !(type == typeof(ushort)) && !(type == typeof(uint)) && !(type == typeof(ulong)))
263 {
264 return type == JsonTypeInfo.ObjectType;
265 }
266 return true;
267 }
268
270 {
271 return (TAttribute)memberInfo.GetCustomAttribute(typeof(TAttribute), inherit: false);
272 }
273
275
277
278 internal abstract object GetValueAsObject(object obj);
279
291
293
295 {
298 {
299 if (reader.TokenType == JsonTokenType.Null)
300 {
301 dictionary[state.Current.JsonPropertyNameAsString] = null;
302 }
303 else
304 {
306 object value = jsonConverter.Read(ref reader, JsonTypeInfo.ObjectType, Options);
307 dictionary[state.Current.JsonPropertyNameAsString] = value;
308 }
309 }
311 {
315 dictionary2[state.Current.JsonPropertyNameAsString] = value2;
316 }
317 else
318 {
319 ConverterBase.ReadElementAndSetProperty(valueAsObject, state.Current.JsonPropertyNameAsString, ref reader, Options, ref state);
320 }
321 return true;
323 {
325 if (elementTypeInfo != null)
326 {
327 return elementTypeInfo.PropertyInfoForTypeInfo.ConverterBase;
328 }
330 }
331 }
332
333 internal abstract bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader);
334
335 internal abstract bool ReadJsonAsObject(ref ReadStack state, ref Utf8JsonReader reader, out object value);
336
338 {
339 if (RuntimeTypeInfo.ElementType == JsonTypeInfo.ObjectType && reader.TokenType == JsonTokenType.Null)
340 {
341 value = null;
342 return true;
343 }
345 if (!jsonConverter.TryRead(ref reader, typeof(JsonElement), Options, ref state, out var value2))
346 {
347 value = null;
348 return false;
349 }
350 value = value2;
351 return true;
352 }
353
354 internal abstract void SetExtensionDictionaryAsObject(object obj, object extensionDict);
355}
356internal sealed class JsonPropertyInfo<T> : JsonPropertyInfo
357{
359
361
362 internal Func<object, T> Get { get; set; }
363
364 internal Action<object, T> Set { get; set; }
365
366 internal override object DefaultValue => default(T);
367
368 public JsonConverter<T> Converter { get; internal set; }
369
370 internal override JsonConverter ConverterBase
371 {
372 get
373 {
374 return Converter;
375 }
376 set
377 {
379 }
380 }
381
383 {
385 if (!(memberInfo is PropertyInfo propertyInfo))
386 {
388 {
389 base.HasGetter = true;
390 Get = options.MemberAccessorStrategy.CreateFieldGetter<T>(fieldInfo);
391 if (!fieldInfo.IsInitOnly)
392 {
393 base.HasSetter = true;
394 Set = options.MemberAccessorStrategy.CreateFieldSetter<T>(fieldInfo);
395 }
397 }
398 else
399 {
400 base.IsForTypeInfo = true;
401 base.HasGetter = true;
402 base.HasSetter = true;
403 }
404 }
405 else
406 {
407 bool flag = JsonPropertyInfo.GetAttribute<JsonIncludeAttribute>(propertyInfo) != null;
408 MethodInfo getMethod = propertyInfo.GetMethod;
409 if (getMethod != null && (getMethod.IsPublic || flag))
410 {
411 base.HasGetter = true;
412 Get = options.MemberAccessorStrategy.CreatePropertyGetter<T>(propertyInfo);
413 }
414 MethodInfo setMethod = propertyInfo.SetMethod;
415 if (setMethod != null && (setMethod.IsPublic || flag))
416 {
417 base.HasSetter = true;
418 Set = options.MemberAccessorStrategy.CreatePropertySetter<T>(propertyInfo);
419 }
420 base.MemberType = MemberTypes.Property;
421 }
423 base.PropertyTypeCanBeNull = base.DeclaredPropertyType.CanBeNull();
424 _propertyTypeEqualsTypeToConvert = typeof(T) == base.DeclaredPropertyType;
426 }
427
429 {
431 base.ClrName = propertyInfo.PropertyName;
432 if (propertyInfo.JsonPropertyName != null)
433 {
434 base.NameAsString = propertyInfo.JsonPropertyName;
435 }
436 else if (options.PropertyNamingPolicy == null)
437 {
438 base.NameAsString = base.ClrName;
439 }
440 else
441 {
442 base.NameAsString = options.PropertyNamingPolicy.ConvertName(base.ClrName);
443 if (base.NameAsString == null)
444 {
446 }
447 }
448 if (base.NameAsUtf8Bytes == null)
449 {
450 byte[] array = (base.NameAsUtf8Bytes = Encoding.UTF8.GetBytes(base.NameAsString));
451 }
452 if (base.EscapedNameSection == null)
453 {
454 byte[] array = (base.EscapedNameSection = JsonHelpers.GetEscapedPropertyNameSection(base.NameAsUtf8Bytes, base.Options.Encoder));
455 }
456 base.SrcGen_IsPublic = propertyInfo.IsPublic;
457 base.SrcGen_HasJsonInclude = propertyInfo.HasJsonInclude;
458 base.SrcGen_IsExtensionData = propertyInfo.IsExtensionData;
460 JsonTypeInfo propertyTypeInfo = propertyInfo.PropertyTypeInfo;
461 Type declaringType = propertyInfo.DeclaringType;
462 JsonConverter jsonConverter = propertyInfo.Converter;
463 if (jsonConverter == null)
464 {
466 if (jsonConverter == null)
467 {
469 }
470 }
472 if (propertyInfo.IgnoreCondition == JsonIgnoreCondition.Always)
473 {
474 base.IsIgnored = true;
475 return;
476 }
477 Get = propertyInfo.Getter;
478 Set = propertyInfo.Setter;
479 base.HasGetter = Get != null;
480 base.HasSetter = Set != null;
483 base.IgnoreCondition = propertyInfo.IgnoreCondition;
486 base.PropertyTypeCanBeNull = typeof(T).CanBeNull();
488 ConverterStrategy = Converter.ConverterStrategy;
489 base.RuntimePropertyType = base.DeclaredPropertyType;
490 DetermineIgnoreCondition(base.IgnoreCondition);
491 DetermineNumberHandlingForProperty(propertyInfo.NumberHandling, null);
493 }
494
510
511 internal override object GetValueAsObject(object obj)
512 {
513 if (base.IsForTypeInfo)
514 {
515 return obj;
516 }
517 return Get(obj);
518 }
519
521 {
522 T value = Get(obj);
523 if (!typeof(T).IsValueType && base.Options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles && value != null && (Converter.CanBePolymorphic || ConverterStrategy != ConverterStrategy.Value) && state.ReferenceResolver.ContainsReferenceForCycleDetection(value))
524 {
525 value = default(T);
526 }
527 if (base.IgnoreDefaultValuesOnWrite)
528 {
529 if (value == null)
530 {
531 return true;
532 }
533 if (!base.PropertyTypeCanBeNull)
534 {
536 {
537 if (EqualityComparer<T>.Default.Equals(default(T), value))
538 {
539 return true;
540 }
541 }
542 else if (base.RuntimeTypeInfo.GenericMethods.IsDefaultValue(value))
543 {
544 return true;
545 }
546 }
547 }
548 if (value == null)
549 {
550 if (Converter.HandleNullOnWrite)
551 {
552 if ((int)state.Current.PropertyState < 2)
553 {
555 writer.WritePropertyNameSection(base.EscapedNameSection);
556 }
557 int currentDepth = writer.CurrentDepth;
558 Converter.Write(writer, value, base.Options);
559 if (currentDepth != writer.CurrentDepth)
560 {
562 }
563 }
564 else
565 {
566 writer.WriteNullSection(base.EscapedNameSection);
567 }
568 return true;
569 }
570 if ((int)state.Current.PropertyState < 2)
571 {
573 writer.WritePropertyNameSection(base.EscapedNameSection);
574 }
575 return Converter.TryWrite(writer, in value, base.Options, ref state);
576 }
577
579 {
580 T val = Get(obj);
581 if (val == null)
582 {
583 return true;
584 }
585 return Converter.TryWriteDataExtensionProperty(writer, val, base.Options, ref state);
586 }
587
588 internal override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader)
589 {
590 bool flag = reader.TokenType == JsonTokenType.Null;
591 bool flag2;
592 if (flag && !Converter.HandleNullOnRead && !state.IsContinuation)
593 {
594 if (!base.PropertyTypeCanBeNull)
595 {
597 }
598 if (!base.IgnoreDefaultValuesOnRead)
599 {
600 Set(obj, default(T));
601 }
602 flag2 = true;
603 }
604 else if (Converter.CanUseDirectReadOrWrite && !state.Current.NumberHandling.HasValue)
605 {
606 if (!flag || !base.IgnoreDefaultValuesOnRead || !base.PropertyTypeCanBeNull)
607 {
608 T arg = Converter.Read(ref reader, base.RuntimePropertyType, base.Options);
609 Set(obj, arg);
610 }
611 flag2 = true;
612 }
613 else
614 {
615 flag2 = true;
616 if (!flag || !base.IgnoreDefaultValuesOnRead || !base.PropertyTypeCanBeNull || state.IsContinuation)
617 {
618 flag2 = Converter.TryRead(ref reader, base.RuntimePropertyType, base.Options, ref state, out var value);
619 if (flag2)
620 {
622 {
623 if (value != null)
624 {
626 if (!base.DeclaredPropertyType.IsAssignableFrom(type))
627 {
629 }
630 }
631 else if (!base.PropertyTypeCanBeNull)
632 {
634 }
635 }
636 Set(obj, value);
637 }
638 }
639 }
640 return flag2;
641 }
642
643 internal override bool ReadJsonAsObject(ref ReadStack state, ref Utf8JsonReader reader, out object value)
644 {
645 bool result;
646 if (reader.TokenType == JsonTokenType.Null && !Converter.HandleNullOnRead && !state.IsContinuation)
647 {
648 if (!base.PropertyTypeCanBeNull)
649 {
651 }
652 value = default(T);
653 result = true;
654 }
655 else if (Converter.CanUseDirectReadOrWrite && !state.Current.NumberHandling.HasValue)
656 {
657 value = Converter.Read(ref reader, base.RuntimePropertyType, base.Options);
658 result = true;
659 }
660 else
661 {
662 result = Converter.TryRead(ref reader, base.RuntimePropertyType, base.Options, ref state, out var value2);
663 value = value2;
664 }
665 return result;
666 }
667
668 internal override void SetExtensionDictionaryAsObject(object obj, object extensionDict)
669 {
670 T arg = (T)extensionDict;
671 Set(obj, arg);
672 }
673}
static string ConverterForPropertyMustBeValid
Definition SR.cs:308
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
static byte[] GetEscapedPropertyNameSection(ReadOnlySpan< byte > utf8Value, JavaScriptEncoder encoder)
string ConvertName(string name)
JsonConverter GetConverterInternal(Type typeToConvert)
virtual void ReadElementAndSetProperty(object obj, string propertyName, ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state)
override bool ReadJsonAsObject(ref ReadStack state, ref Utf8JsonReader reader, out object value)
virtual void Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ConverterStrategy runtimeClassType, MemberInfo memberInfo, bool isVirtual, JsonConverter converter, JsonIgnoreCondition? ignoreCondition, JsonNumberHandling? parentTypeNumberHandling, JsonSerializerOptions options)
void InitializeForSourceGen(JsonSerializerOptions options, JsonPropertyInfoValues< T > propertyInfo)
override bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
bool ReadJsonAndAddExtensionProperty(object obj, ref ReadStack state, ref Utf8JsonReader reader)
override bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader)
bool ReadJsonAsObject(ref ReadStack state, ref Utf8JsonReader reader, out object value)
void DetermineIgnoreCondition(JsonIgnoreCondition? ignoreCondition)
static JsonPropertyInfo CreateIgnoredPropertyPlaceholder(MemberInfo memberInfo, Type memberType, bool isVirtual, JsonSerializerOptions options)
override void Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ConverterStrategy runtimeClassType, MemberInfo memberInfo, bool isVirtual, JsonConverter converter, JsonIgnoreCondition? ignoreCondition, JsonNumberHandling? parentTypeNumberHandling, JsonSerializerOptions options)
bool ReadJsonExtensionDataValue(ref ReadStack state, ref Utf8JsonReader reader, out object value)
void SetExtensionDictionaryAsObject(object obj, object extensionDict)
bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
bool ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader)
bool GetMemberAndWriteJsonExtensionData(object obj, ref WriteStack state, Utf8JsonWriter writer)
void DetermineSerializationCapabilities(JsonIgnoreCondition? ignoreCondition)
override void InitializeForTypeInfo(Type declaredType, JsonTypeInfo runtimeTypeInfo, JsonConverter converter, JsonSerializerOptions options)
override bool GetMemberAndWriteJsonExtensionData(object obj, ref WriteStack state, Utf8JsonWriter writer)
override void SetExtensionDictionaryAsObject(object obj, object extensionDict)
void DetermineNumberHandlingForTypeInfo(JsonNumberHandling? numberHandling)
void InitializeForTypeInfo(Type declaredType, JsonTypeInfo runtimeTypeInfo, JsonConverter converter, JsonSerializerOptions options)
virtual void GetPolicies(JsonIgnoreCondition? ignoreCondition, JsonNumberHandling? declaringTypeNumberHandling)
void DetermineNumberHandlingForProperty(JsonNumberHandling? propertyNumberHandling, JsonNumberHandling? declaringTypeNumberHandling)
static TAttribute GetAttribute< TAttribute >(MemberInfo memberInfo)
static void ThrowInvalidOperationException_SerializerPropertyNameNull(Type parentType, JsonPropertyInfo jsonPropertyInfo)
static void ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(JsonPropertyInfo jsonPropertyInfo)
static void ThrowInvalidCastException_DeserializeUnableToAssignValue(Type typeOfValue, Type declaredType)
static void ThrowInvalidOperationException_DeserializeUnableToAssignNull(Type declaredType)
static void ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
static void ThrowInvalidOperationException_IgnoreConditionOnValueTypeInvalid(string clrPropertyName, Type propertyDeclaringType)
static void ThrowJsonException_SerializationConverterWrite(JsonConverter converter)
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static ? Type GetUnderlyingType(Type nullableType)
Definition Nullable.cs:139