Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonSerializerOptions.cs
Go to the documentation of this file.
13
14namespace System.Text.Json;
15
16public sealed class JsonSerializerOptions
17{
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
56 private int _defaultBufferSize = 16384;
57
58 private int _maxDepth;
59
61
63
64 private bool _ignoreNullValues;
65
67
69
70 private bool _includeFields;
71
73
74 private bool _writeIndented;
75
77
78 public IList<JsonConverter> Converters { get; }
79
80 private JsonTypeInfo? _lastClass { get; set; }
81
83 {
84 get
85 {
87 }
88 set
89 {
92 }
93 }
94
96 {
97 get
98 {
99 return _defaultBufferSize;
100 }
101 set
102 {
104 if (value < 1)
105 {
107 }
109 }
110 }
111
113 {
114 get
115 {
116 return _encoder;
117 }
118 set
119 {
121 _encoder = value;
122 }
123 }
124
126 {
127 get
128 {
130 }
131 set
132 {
135 }
136 }
137
138 [Obsolete("JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.", DiagnosticId = "SYSLIB0020", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
141 {
142 get
143 {
144 return _ignoreNullValues;
145 }
146 set
147 {
149 if (value && _defaultIgnoreCondition != 0)
150 {
152 }
154 }
155 }
156
158 {
159 get
160 {
162 }
163 set
164 {
166 switch (value)
167 {
168 case JsonIgnoreCondition.Always:
170 default:
172 {
174 }
175 break;
176 case JsonIgnoreCondition.Never:
177 break;
178 }
180 }
181 }
182
184 {
185 get
186 {
187 return _numberHandling;
188 }
189 set
190 {
193 {
194 throw new ArgumentOutOfRangeException("value");
195 }
197 }
198 }
199
201 {
202 get
203 {
205 }
206 set
207 {
210 }
211 }
212
214 {
215 get
216 {
218 }
219 set
220 {
223 }
224 }
225
226 public bool IncludeFields
227 {
228 get
229 {
230 return _includeFields;
231 }
232 set
233 {
236 }
237 }
238
239 public int MaxDepth
240 {
241 get
242 {
243 return _maxDepth;
244 }
245 set
246 {
248 if (value < 0)
249 {
251 }
253 EffectiveMaxDepth = ((value == 0) ? 64 : value);
254 }
255 }
256
257 internal int EffectiveMaxDepth { get; private set; } = 64;
258
259
261 {
262 get
263 {
265 }
266 set
267 {
270 }
271 }
272
274 {
275 get
276 {
278 }
279 set
280 {
283 }
284 }
285
287 {
288 get
289 {
291 }
292 set
293 {
295 if ((int)value > 1)
296 {
298 }
300 }
301 }
302
304 {
305 get
306 {
308 }
309 set
310 {
313 }
314 }
315
316 public bool WriteIndented
317 {
318 get
319 {
320 return _writeIndented;
321 }
322 set
323 {
326 }
327 }
328
330 {
331 get
332 {
333 return _referenceHandler;
334 }
335 set
336 {
340 }
341 }
342
354
355 internal bool IsInitializedForReflectionSerializer { get; set; }
356
357 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
373
407
433
434 [RequiresUnreferencedCode("Getting a converter for a type may require reflection which depends on unreferenced code.")]
436 {
437 if (typeToConvert == null)
438 {
439 throw new ArgumentNullException("typeToConvert");
440 }
443 }
444
446 {
448 {
449 return value;
450 }
452 foreach (JsonConverter converter in Converters)
453 {
454 if (converter.CanConvert(typeToConvert))
455 {
457 break;
458 }
459 }
460 if (value == null)
461 {
463 if (jsonConverterAttribute != null)
464 {
466 }
467 }
468 if (value == null)
469 {
471 {
473 return null;
474 }
476 {
477 value = value2;
478 }
479 else
480 {
483 {
484 if (jsonConverter.CanConvert(typeToConvert))
485 {
487 break;
488 }
489 }
490 }
491 }
493 {
494 value = jsonConverterFactory.GetConverterInternal(typeToConvert, this);
495 }
496 Type typeToConvert2 = value.TypeToConvert;
497 if (!typeToConvert2.IsAssignableFromInternal(typeToConvert) && !typeToConvert.IsAssignableFromInternal(typeToConvert2))
498 {
500 }
502 {
504 }
505 return value;
506 }
507
509 {
510 Type converterType = converterAttribute.ConverterType;
512 if (converterType == null)
513 {
515 if (jsonConverter == null)
516 {
518 }
519 }
520 else
521 {
523 if (!typeof(JsonConverter).IsAssignableFrom(converterType) || constructor == null || !constructor.IsPublic)
524 {
526 }
528 }
529 if (!jsonConverter.CanConvert(typeToConvert))
530 {
532 if (underlyingType != null && jsonConverter.CanConvert(underlyingType))
533 {
535 {
536 jsonConverter = jsonConverterFactory.GetConverterInternal(underlyingType, this);
537 }
539 }
541 }
542 return jsonConverter;
543 }
544
546 {
548 {
549 return true;
550 }
551 converter = null;
552 return false;
553 }
554
556 {
557 object[] customAttributes = memberInfo.GetCustomAttributes(attributeType, inherit: false);
559 }
560
562 {
563 object[] customAttributes = classType.GetCustomAttributes(attributeType, inherit: false);
564 return GetAttributeThatCanHaveMultiple(attributeType, classType, null, customAttributes);
565 }
566
567 private static Attribute GetAttributeThatCanHaveMultiple(Type attributeType, Type classType, MemberInfo memberInfo, object[] attributes)
568 {
569 if (attributes.Length == 0)
570 {
571 return null;
572 }
573 if (attributes.Length == 1)
574 {
575 return (Attribute)attributes[0];
576 }
578 return null;
579 }
580
582 {
583 Converters = new ConverterList(this);
585 }
586
588 {
589 if (options == null)
590 {
591 throw new ArgumentNullException("options");
592 }
593 _memberAccessorStrategy = options._memberAccessorStrategy;
594 _dictionaryKeyPolicy = options._dictionaryKeyPolicy;
595 _jsonPropertyNamingPolicy = options._jsonPropertyNamingPolicy;
596 _readCommentHandling = options._readCommentHandling;
597 _referenceHandler = options._referenceHandler;
598 _encoder = options._encoder;
599 _defaultIgnoreCondition = options._defaultIgnoreCondition;
600 _numberHandling = options._numberHandling;
601 _unknownTypeHandling = options._unknownTypeHandling;
602 _defaultBufferSize = options._defaultBufferSize;
603 _maxDepth = options._maxDepth;
604 _allowTrailingCommas = options._allowTrailingCommas;
605 _ignoreNullValues = options._ignoreNullValues;
606 _ignoreReadOnlyProperties = options._ignoreReadOnlyProperties;
607 _ignoreReadonlyFields = options._ignoreReadonlyFields;
608 _includeFields = options._includeFields;
609 _propertyNameCaseInsensitive = options._propertyNameCaseInsensitive;
610 _writeIndented = options._writeIndented;
611 Converters = new ConverterList(this, (ConverterList)options.Converters);
612 EffectiveMaxDepth = options.EffectiveMaxDepth;
613 ReferenceHandlingStrategy = options.ReferenceHandlingStrategy;
615 }
616
621
623 : this()
624 {
625 switch (defaults)
626 {
627 case JsonSerializerDefaults.Web:
630 _numberHandling = JsonNumberHandling.AllowReadingFromString;
631 break;
632 default:
633 throw new ArgumentOutOfRangeException("defaults");
634 case JsonSerializerDefaults.General:
635 break;
636 }
637 }
638
647
648 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
650 {
654 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
656 {
657 return new JsonTypeInfo(type, options);
658 }
659 }
660
662 {
665 {
667 }
668 return jsonTypeInfo;
669 }
670
672 {
674 if (jsonTypeInfo != null)
675 {
676 return jsonTypeInfo;
677 }
678 if (_typeInfoCreationFunc == null)
679 {
681 return null;
682 }
683 return _typeInfoCreationFunc(type, this);
684 }
685
686 [MethodImpl(MethodImplOptions.AggressiveInlining)]
696
698 {
700 {
701 jsonTypeInfo = null;
702 return false;
703 }
705 return true;
706 }
707
708 internal bool TypeIsCached(Type type)
709 {
710 return _classes.ContainsKey(type);
711 }
712
713 internal void ClearClasses()
714 {
715 _classes.Clear();
716 _lastClass = null;
717 }
718
727
734
743
745 {
746 JsonWriterOptions result = default(JsonWriterOptions);
750 return result;
751 }
752
760}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool TryAdd(TKey key, TValue value)
void Add(TKey key, TValue value)
static string DefaultIgnoreConditionAlreadySpecified
Definition SR.cs:264
static string DefaultIgnoreConditionInvalid
Definition SR.cs:266
static string SerializationInvalidBufferSize
Definition SR.cs:134
static string JsonSerializerDoesNotSupportComments
Definition SR.cs:180
Definition SR.cs:7
static JsonNamingPolicy CamelCase
static ConditionalWeakTable< JsonSerializerOptions, object > All
static Dictionary< Type, JsonConverter > GetDefaultSimpleConverters()
static Attribute GetAttributeThatCanHaveMultiple(Type attributeType, Type classType, MemberInfo memberInfo, object[] attributes)
static void TrackOptionsInstance(JsonSerializerOptions options)
static Dictionary< Type, JsonConverter > s_defaultSimpleConverters
JsonConverter GetConverter(Type typeToConvert)
static readonly JsonSerializerOptions s_defaultOptions
JsonSerializerOptions(JsonSerializerDefaults defaults)
JsonConverter DetermineConverter(Type parentClassType, Type runtimePropertyType, MemberInfo memberInfo)
ReferenceHandlingStrategy ReferenceHandlingStrategy
static Attribute GetAttributeThatCanHaveMultiple(Type classType, Type attributeType, MemberInfo memberInfo)
bool TryGetClass(Type type, [NotNullWhen(true)] out JsonTypeInfo jsonTypeInfo)
JsonConverter GetConverterFromAttribute(JsonConverterAttribute converterAttribute, Type typeToConvert, Type classTypeAttributeIsOn, MemberInfo memberInfo)
readonly ConcurrentDictionary< Type, JsonConverter > _converters
static Attribute GetAttributeThatCanHaveMultiple(Type classType, Type attributeType)
Func< Type, JsonSerializerOptions, JsonTypeInfo > _typeInfoCreationFunc
JsonConverter GetConverterInternal(Type typeToConvert)
readonly ConcurrentDictionary< Type, JsonTypeInfo > _classes
JsonSerializerOptions(JsonSerializerOptions options)
bool TryGetDefaultSimpleConverter(Type typeToConvert, [NotNullWhen(true)] out JsonConverter converter)
static bool IsValidNumberHandlingValue(JsonNumberHandling handling)
static JsonConverter CreateValueConverter(Type valueTypeToConvert, JsonConverter valueConverter)
static void ThrowInvalidOperationException_SerializerOptionsImmutable(JsonSerializerContext context)
static void ThrowNotSupportedException_BuiltInConvertersNotRooted(Type type)
static void ThrowInvalidOperationException_SerializationConverterNotCompatible(Type converterType, Type type)
static void ThrowInvalidOperationException_SerializationConverterOnAttributeNotCompatible(Type classTypeAttributeIsOn, MemberInfo memberInfo, Type typeToConvert)
static void ThrowInvalidOperationException_SerializationDuplicateAttribute(Type attribute, Type classType, MemberInfo memberInfo)
static ArgumentOutOfRangeException GetArgumentOutOfRangeException_MaxDepthMustBePositive(string parameterName)
static void ThrowInvalidOperationException_JsonSerializerOptionsAlreadyBoundToContext()
static void ThrowInvalidOperationException_SerializationConverterOnAttributeInvalid(Type classType, MemberInfo memberInfo)
static void ThrowNotSupportedException_NoMetadataForType(Type type)
static void ThrowInvalidOperationException_ConverterCanConvertMultipleTypes(Type runtimePropertyType, JsonConverter jsonConverter)
static readonly Type[] EmptyTypes
Definition Type.cs:19
static ? Type GetUnderlyingType(Type nullableType)
Definition Nullable.cs:139