/
JsonSerializerSettings.cs
456 lines (415 loc) · 20.1 KB
/
JsonSerializerSettings.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
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json.Serialization;
using System.Runtime.Serialization;
using System.Diagnostics;
namespace Newtonsoft.Json
{
/// <summary>
/// Specifies the settings on a <see cref="JsonSerializer"/> object.
/// </summary>
public class JsonSerializerSettings
{
internal const ReferenceLoopHandling DefaultReferenceLoopHandling = ReferenceLoopHandling.Error;
internal const MissingMemberHandling DefaultMissingMemberHandling = MissingMemberHandling.Ignore;
internal const NullValueHandling DefaultNullValueHandling = NullValueHandling.Include;
internal const DefaultValueHandling DefaultDefaultValueHandling = DefaultValueHandling.Include;
internal const ObjectCreationHandling DefaultObjectCreationHandling = ObjectCreationHandling.Auto;
internal const PreserveReferencesHandling DefaultPreserveReferencesHandling = PreserveReferencesHandling.None;
internal const ConstructorHandling DefaultConstructorHandling = ConstructorHandling.Default;
internal const TypeNameHandling DefaultTypeNameHandling = TypeNameHandling.None;
internal const MetadataPropertyHandling DefaultMetadataPropertyHandling = MetadataPropertyHandling.Default;
internal static readonly StreamingContext DefaultContext;
internal const Formatting DefaultFormatting = Formatting.None;
internal const DateFormatHandling DefaultDateFormatHandling = DateFormatHandling.IsoDateFormat;
internal const DateTimeZoneHandling DefaultDateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
internal const DateParseHandling DefaultDateParseHandling = DateParseHandling.DateTime;
internal const FloatParseHandling DefaultFloatParseHandling = FloatParseHandling.Double;
internal const FloatFormatHandling DefaultFloatFormatHandling = FloatFormatHandling.String;
internal const StringEscapeHandling DefaultStringEscapeHandling = StringEscapeHandling.Default;
internal const TypeNameAssemblyFormatHandling DefaultTypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple;
internal static readonly CultureInfo DefaultCulture;
internal const bool DefaultCheckAdditionalContent = false;
internal const string DefaultDateFormatString = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
internal const int DefaultMaxDepth = 64;
internal Formatting? _formatting;
internal DateFormatHandling? _dateFormatHandling;
internal DateTimeZoneHandling? _dateTimeZoneHandling;
internal DateParseHandling? _dateParseHandling;
internal FloatFormatHandling? _floatFormatHandling;
internal FloatParseHandling? _floatParseHandling;
internal StringEscapeHandling? _stringEscapeHandling;
internal CultureInfo? _culture;
internal bool? _checkAdditionalContent;
internal int? _maxDepth;
internal bool _maxDepthSet;
internal string? _dateFormatString;
internal bool _dateFormatStringSet;
internal TypeNameAssemblyFormatHandling? _typeNameAssemblyFormatHandling;
internal DefaultValueHandling? _defaultValueHandling;
internal PreserveReferencesHandling? _preserveReferencesHandling;
internal NullValueHandling? _nullValueHandling;
internal ObjectCreationHandling? _objectCreationHandling;
internal MissingMemberHandling? _missingMemberHandling;
internal ReferenceLoopHandling? _referenceLoopHandling;
internal StreamingContext? _context;
internal ConstructorHandling? _constructorHandling;
internal TypeNameHandling? _typeNameHandling;
internal MetadataPropertyHandling? _metadataPropertyHandling;
/// <summary>
/// Gets or sets how reference loops (e.g. a class referencing itself) are handled.
/// The default value is <see cref="Json.ReferenceLoopHandling.Error" />.
/// </summary>
/// <value>Reference loop handling.</value>
public ReferenceLoopHandling ReferenceLoopHandling
{
get => _referenceLoopHandling ?? DefaultReferenceLoopHandling;
set => _referenceLoopHandling = value;
}
/// <summary>
/// Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.
/// The default value is <see cref="Json.MissingMemberHandling.Ignore" />.
/// </summary>
/// <value>Missing member handling.</value>
public MissingMemberHandling MissingMemberHandling
{
get => _missingMemberHandling ?? DefaultMissingMemberHandling;
set => _missingMemberHandling = value;
}
/// <summary>
/// Gets or sets how objects are created during deserialization.
/// The default value is <see cref="Json.ObjectCreationHandling.Auto" />.
/// </summary>
/// <value>The object creation handling.</value>
public ObjectCreationHandling ObjectCreationHandling
{
get => _objectCreationHandling ?? DefaultObjectCreationHandling;
set => _objectCreationHandling = value;
}
/// <summary>
/// Gets or sets how null values are handled during serialization and deserialization.
/// The default value is <see cref="Json.NullValueHandling.Include" />.
/// </summary>
/// <value>Null value handling.</value>
public NullValueHandling NullValueHandling
{
get => _nullValueHandling ?? DefaultNullValueHandling;
set => _nullValueHandling = value;
}
/// <summary>
/// Gets or sets how default values are handled during serialization and deserialization.
/// The default value is <see cref="Json.DefaultValueHandling.Include" />.
/// </summary>
/// <value>The default value handling.</value>
public DefaultValueHandling DefaultValueHandling
{
get => _defaultValueHandling ?? DefaultDefaultValueHandling;
set => _defaultValueHandling = value;
}
/// <summary>
/// Gets or sets a <see cref="JsonConverter"/> collection that will be used during serialization.
/// </summary>
/// <value>The converters.</value>
public IList<JsonConverter> Converters { get; set; }
/// <summary>
/// Gets or sets how object references are preserved by the serializer.
/// The default value is <see cref="Json.PreserveReferencesHandling.None" />.
/// </summary>
/// <value>The preserve references handling.</value>
public PreserveReferencesHandling PreserveReferencesHandling
{
get => _preserveReferencesHandling ?? DefaultPreserveReferencesHandling;
set => _preserveReferencesHandling = value;
}
/// <summary>
/// Gets or sets how type name writing and reading is handled by the serializer.
/// The default value is <see cref="Json.TypeNameHandling.None" />.
/// </summary>
/// <remarks>
/// <see cref="JsonSerializerSettings.TypeNameHandling"/> should be used with caution when your application deserializes JSON from an external source.
/// Incoming types should be validated with a custom <see cref="JsonSerializerSettings.SerializationBinder"/>
/// when deserializing with a value other than <see cref="Json.TypeNameHandling.None"/>.
/// </remarks>
/// <value>The type name handling.</value>
public TypeNameHandling TypeNameHandling
{
get => _typeNameHandling ?? DefaultTypeNameHandling;
set => _typeNameHandling = value;
}
/// <summary>
/// Gets or sets how metadata properties are used during deserialization.
/// The default value is <see cref="Json.MetadataPropertyHandling.Default" />.
/// </summary>
/// <value>The metadata properties handling.</value>
public MetadataPropertyHandling MetadataPropertyHandling
{
get => _metadataPropertyHandling ?? DefaultMetadataPropertyHandling;
set => _metadataPropertyHandling = value;
}
/// <summary>
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// The default value is <see cref="FormatterAssemblyStyle.Simple" />.
/// </summary>
/// <value>The type name assembly format.</value>
[Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
public FormatterAssemblyStyle TypeNameAssemblyFormat
{
get => (FormatterAssemblyStyle)TypeNameAssemblyFormatHandling;
set => TypeNameAssemblyFormatHandling = (TypeNameAssemblyFormatHandling)value;
}
/// <summary>
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// The default value is <see cref="Json.TypeNameAssemblyFormatHandling.Simple" />.
/// </summary>
/// <value>The type name assembly format.</value>
public TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
{
get => _typeNameAssemblyFormatHandling ?? DefaultTypeNameAssemblyFormatHandling;
set => _typeNameAssemblyFormatHandling = value;
}
/// <summary>
/// Gets or sets how constructors are used during deserialization.
/// The default value is <see cref="Json.ConstructorHandling.Default" />.
/// </summary>
/// <value>The constructor handling.</value>
public ConstructorHandling ConstructorHandling
{
get => _constructorHandling ?? DefaultConstructorHandling;
set => _constructorHandling = value;
}
/// <summary>
/// Gets or sets the contract resolver used by the serializer when
/// serializing .NET objects to JSON and vice versa.
/// </summary>
/// <value>The contract resolver.</value>
public IContractResolver? ContractResolver { get; set; }
/// <summary>
/// Gets or sets the equality comparer used by the serializer when comparing references.
/// </summary>
/// <value>The equality comparer.</value>
public IEqualityComparer? EqualityComparer { get; set; }
/// <summary>
/// Gets or sets the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
/// </summary>
/// <value>The reference resolver.</value>
[Obsolete("ReferenceResolver property is obsolete. Use the ReferenceResolverProvider property to set the IReferenceResolver: settings.ReferenceResolverProvider = () => resolver")]
public IReferenceResolver? ReferenceResolver
{
get => ReferenceResolverProvider?.Invoke();
set
{
ReferenceResolverProvider = (value != null)
? () => value
: (Func<IReferenceResolver?>?)null;
}
}
/// <summary>
/// Gets or sets a function that creates the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
/// </summary>
/// <value>A function that creates the <see cref="IReferenceResolver"/> used by the serializer when resolving references.</value>
public Func<IReferenceResolver?>? ReferenceResolverProvider { get; set; }
/// <summary>
/// Gets or sets the <see cref="ITraceWriter"/> used by the serializer when writing trace messages.
/// </summary>
/// <value>The trace writer.</value>
public ITraceWriter? TraceWriter { get; set; }
/// <summary>
/// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names.
/// </summary>
/// <value>The binder.</value>
[Obsolete("Binder is obsolete. Use SerializationBinder instead.")]
public SerializationBinder? Binder
{
get
{
if (SerializationBinder == null)
{
return null;
}
if (SerializationBinder is SerializationBinderAdapter adapter)
{
return adapter.SerializationBinder;
}
throw new InvalidOperationException("Cannot get SerializationBinder because an ISerializationBinder was previously set.");
}
set => SerializationBinder = value == null ? null : new SerializationBinderAdapter(value);
}
/// <summary>
/// Gets or sets the <see cref="ISerializationBinder"/> used by the serializer when resolving type names.
/// </summary>
/// <value>The binder.</value>
public ISerializationBinder? SerializationBinder { get; set; }
/// <summary>
/// Gets or sets the error handler called during serialization and deserialization.
/// </summary>
/// <value>The error handler called during serialization and deserialization.</value>
public EventHandler<ErrorEventArgs>? Error { get; set; }
/// <summary>
/// Gets or sets the <see cref="StreamingContext"/> used by the serializer when invoking serialization callback methods.
/// </summary>
/// <value>The context.</value>
public StreamingContext Context
{
get => _context ?? DefaultContext;
set => _context = value;
}
/// <summary>
/// Gets or sets how <see cref="DateTime"/> and <see cref="DateTimeOffset"/> values are formatted when writing JSON text,
/// and the expected date format when reading JSON text.
/// The default value is <c>"yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"</c>.
/// </summary>
public string DateFormatString
{
get => _dateFormatString ?? DefaultDateFormatString;
set
{
_dateFormatString = value;
_dateFormatStringSet = true;
}
}
/// <summary>
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
/// A null value means there is no maximum.
/// The default value is <c>128</c>.
/// </summary>
public int? MaxDepth
{
get => _maxDepthSet ? _maxDepth : DefaultMaxDepth;
set
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
_maxDepthSet = true;
}
}
/// <summary>
/// Indicates how JSON text output is formatted.
/// The default value is <see cref="Json.Formatting.None" />.
/// </summary>
public Formatting Formatting
{
get => _formatting ?? DefaultFormatting;
set => _formatting = value;
}
/// <summary>
/// Gets or sets how dates are written to JSON text.
/// The default value is <see cref="Json.DateFormatHandling.IsoDateFormat" />.
/// </summary>
public DateFormatHandling DateFormatHandling
{
get => _dateFormatHandling ?? DefaultDateFormatHandling;
set => _dateFormatHandling = value;
}
/// <summary>
/// Gets or sets how <see cref="DateTime"/> time zones are handled during serialization and deserialization.
/// The default value is <see cref="Json.DateTimeZoneHandling.RoundtripKind" />.
/// </summary>
public DateTimeZoneHandling DateTimeZoneHandling
{
get => _dateTimeZoneHandling ?? DefaultDateTimeZoneHandling;
set => _dateTimeZoneHandling = value;
}
/// <summary>
/// Gets or sets how date formatted strings, e.g. <c>"\/Date(1198908717056)\/"</c> and <c>"2012-03-21T05:40Z"</c>, are parsed when reading JSON.
/// The default value is <see cref="Json.DateParseHandling.DateTime" />.
/// </summary>
public DateParseHandling DateParseHandling
{
get => _dateParseHandling ?? DefaultDateParseHandling;
set => _dateParseHandling = value;
}
/// <summary>
/// Gets or sets how special floating point numbers, e.g. <see cref="Double.NaN"/>,
/// <see cref="Double.PositiveInfinity"/> and <see cref="Double.NegativeInfinity"/>,
/// are written as JSON.
/// The default value is <see cref="Json.FloatFormatHandling.String" />.
/// </summary>
public FloatFormatHandling FloatFormatHandling
{
get => _floatFormatHandling ?? DefaultFloatFormatHandling;
set => _floatFormatHandling = value;
}
/// <summary>
/// Gets or sets how floating point numbers, e.g. 1.0 and 9.9, are parsed when reading JSON text.
/// The default value is <see cref="Json.FloatParseHandling.Double" />.
/// </summary>
public FloatParseHandling FloatParseHandling
{
get => _floatParseHandling ?? DefaultFloatParseHandling;
set => _floatParseHandling = value;
}
/// <summary>
/// Gets or sets how strings are escaped when writing JSON text.
/// The default value is <see cref="Json.StringEscapeHandling.Default" />.
/// </summary>
public StringEscapeHandling StringEscapeHandling
{
get => _stringEscapeHandling ?? DefaultStringEscapeHandling;
set => _stringEscapeHandling = value;
}
/// <summary>
/// Gets or sets the culture used when reading JSON.
/// The default value is <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
public CultureInfo Culture
{
get => _culture ?? DefaultCulture;
set => _culture = value;
}
/// <summary>
/// Gets a value indicating whether there will be a check for additional content after deserializing an object.
/// The default value is <c>false</c>.
/// </summary>
/// <value>
/// <c>true</c> if there will be a check for additional content after deserializing an object; otherwise, <c>false</c>.
/// </value>
public bool CheckAdditionalContent
{
get => _checkAdditionalContent ?? DefaultCheckAdditionalContent;
set => _checkAdditionalContent = value;
}
static JsonSerializerSettings()
{
DefaultContext = new StreamingContext();
DefaultCulture = CultureInfo.InvariantCulture;
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializerSettings"/> class.
/// </summary>
[DebuggerStepThrough]
public JsonSerializerSettings()
{
Converters = new List<JsonConverter>();
}
}
}