#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 LC.Newtonsoft.Json.Serialization;
using System.Runtime.Serialization;
using System.Diagnostics;
namespace LC.Newtonsoft.Json
{
///
/// Specifies the settings on a object.
///
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 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;
///
/// Gets or sets how reference loops (e.g. a class referencing itself) are handled.
/// The default value is .
///
/// Reference loop handling.
public ReferenceLoopHandling ReferenceLoopHandling
{
get => _referenceLoopHandling ?? DefaultReferenceLoopHandling;
set => _referenceLoopHandling = value;
}
///
/// 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 .
///
/// Missing member handling.
public MissingMemberHandling MissingMemberHandling
{
get => _missingMemberHandling ?? DefaultMissingMemberHandling;
set => _missingMemberHandling = value;
}
///
/// Gets or sets how objects are created during deserialization.
/// The default value is .
///
/// The object creation handling.
public ObjectCreationHandling ObjectCreationHandling
{
get => _objectCreationHandling ?? DefaultObjectCreationHandling;
set => _objectCreationHandling = value;
}
///
/// Gets or sets how null values are handled during serialization and deserialization.
/// The default value is .
///
/// Null value handling.
public NullValueHandling NullValueHandling
{
get => _nullValueHandling ?? DefaultNullValueHandling;
set => _nullValueHandling = value;
}
///
/// Gets or sets how default values are handled during serialization and deserialization.
/// The default value is .
///
/// The default value handling.
public DefaultValueHandling DefaultValueHandling
{
get => _defaultValueHandling ?? DefaultDefaultValueHandling;
set => _defaultValueHandling = value;
}
///
/// Gets or sets a collection that will be used during serialization.
///
/// The converters.
public IList Converters { get; set; }
///
/// Gets or sets how object references are preserved by the serializer.
/// The default value is .
///
/// The preserve references handling.
public PreserveReferencesHandling PreserveReferencesHandling
{
get => _preserveReferencesHandling ?? DefaultPreserveReferencesHandling;
set => _preserveReferencesHandling = value;
}
///
/// Gets or sets how type name writing and reading is handled by the serializer.
/// The default value is .
///
///
/// should be used with caution when your application deserializes JSON from an external source.
/// Incoming types should be validated with a custom
/// when deserializing with a value other than .
///
/// The type name handling.
public TypeNameHandling TypeNameHandling
{
get => _typeNameHandling ?? DefaultTypeNameHandling;
set => _typeNameHandling = value;
}
///
/// Gets or sets how metadata properties are used during deserialization.
/// The default value is .
///
/// The metadata properties handling.
public MetadataPropertyHandling MetadataPropertyHandling
{
get => _metadataPropertyHandling ?? DefaultMetadataPropertyHandling;
set => _metadataPropertyHandling = value;
}
///
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// The default value is .
///
/// The type name assembly format.
[Obsolete("TypeNameAssemblyFormat is obsolete. Use TypeNameAssemblyFormatHandling instead.")]
public FormatterAssemblyStyle TypeNameAssemblyFormat
{
get => (FormatterAssemblyStyle)TypeNameAssemblyFormatHandling;
set => TypeNameAssemblyFormatHandling = (TypeNameAssemblyFormatHandling)value;
}
///
/// Gets or sets how a type name assembly is written and resolved by the serializer.
/// The default value is .
///
/// The type name assembly format.
public TypeNameAssemblyFormatHandling TypeNameAssemblyFormatHandling
{
get => _typeNameAssemblyFormatHandling ?? DefaultTypeNameAssemblyFormatHandling;
set => _typeNameAssemblyFormatHandling = value;
}
///
/// Gets or sets how constructors are used during deserialization.
/// The default value is .
///
/// The constructor handling.
public ConstructorHandling ConstructorHandling
{
get => _constructorHandling ?? DefaultConstructorHandling;
set => _constructorHandling = value;
}
///
/// Gets or sets the contract resolver used by the serializer when
/// serializing .NET objects to JSON and vice versa.
///
/// The contract resolver.
public IContractResolver ContractResolver { get; set; }
///
/// Gets or sets the equality comparer used by the serializer when comparing references.
///
/// The equality comparer.
public IEqualityComparer EqualityComparer { get; set; }
///
/// Gets or sets the used by the serializer when resolving references.
///
/// The reference resolver.
[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)null;
}
}
///
/// Gets or sets a function that creates the used by the serializer when resolving references.
///
/// A function that creates the used by the serializer when resolving references.
public Func ReferenceResolverProvider { get; set; }
///
/// Gets or sets the used by the serializer when writing trace messages.
///
/// The trace writer.
public ITraceWriter TraceWriter { get; set; }
///
/// Gets or sets the used by the serializer when resolving type names.
///
/// The binder.
[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);
}
///
/// Gets or sets the used by the serializer when resolving type names.
///
/// The binder.
public ISerializationBinder SerializationBinder { get; set; }
///
/// Gets or sets the error handler called during serialization and deserialization.
///
/// The error handler called during serialization and deserialization.
public EventHandler Error { get; set; }
///
/// Gets or sets the used by the serializer when invoking serialization callback methods.
///
/// The context.
public StreamingContext Context
{
get => _context ?? DefaultContext;
set => _context = value;
}
///
/// Gets or sets how and values are formatted when writing JSON text,
/// and the expected date format when reading JSON text.
/// The default value is "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK".
///
public string DateFormatString
{
get => _dateFormatString ?? DefaultDateFormatString;
set
{
_dateFormatString = value;
_dateFormatStringSet = true;
}
}
///
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a .
/// A null value means there is no maximum.
/// The default value is null.
///
public int? MaxDepth
{
get => _maxDepth;
set
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
_maxDepthSet = true;
}
}
///
/// Indicates how JSON text output is formatted.
/// The default value is .
///
public Formatting Formatting
{
get => _formatting ?? DefaultFormatting;
set => _formatting = value;
}
///
/// Gets or sets how dates are written to JSON text.
/// The default value is .
///
public DateFormatHandling DateFormatHandling
{
get => _dateFormatHandling ?? DefaultDateFormatHandling;
set => _dateFormatHandling = value;
}
///
/// Gets or sets how time zones are handled during serialization and deserialization.
/// The default value is .
///
public DateTimeZoneHandling DateTimeZoneHandling
{
get => _dateTimeZoneHandling ?? DefaultDateTimeZoneHandling;
set => _dateTimeZoneHandling = value;
}
///
/// Gets or sets how date formatted strings, e.g. "\/Date(1198908717056)\/" and "2012-03-21T05:40Z", are parsed when reading JSON.
/// The default value is .
///
public DateParseHandling DateParseHandling
{
get => _dateParseHandling ?? DefaultDateParseHandling;
set => _dateParseHandling = value;
}
///
/// Gets or sets how special floating point numbers, e.g. ,
/// and ,
/// are written as JSON.
/// The default value is .
///
public FloatFormatHandling FloatFormatHandling
{
get => _floatFormatHandling ?? DefaultFloatFormatHandling;
set => _floatFormatHandling = value;
}
///
/// Gets or sets how floating point numbers, e.g. 1.0 and 9.9, are parsed when reading JSON text.
/// The default value is .
///
public FloatParseHandling FloatParseHandling
{
get => _floatParseHandling ?? DefaultFloatParseHandling;
set => _floatParseHandling = value;
}
///
/// Gets or sets how strings are escaped when writing JSON text.
/// The default value is .
///
public StringEscapeHandling StringEscapeHandling
{
get => _stringEscapeHandling ?? DefaultStringEscapeHandling;
set => _stringEscapeHandling = value;
}
///
/// Gets or sets the culture used when reading JSON.
/// The default value is .
///
public CultureInfo Culture
{
get => _culture ?? DefaultCulture;
set => _culture = value;
}
///
/// Gets a value indicating whether there will be a check for additional content after deserializing an object.
/// The default value is false.
///
///
/// true if there will be a check for additional content after deserializing an object; otherwise, false.
///
public bool CheckAdditionalContent
{
get => _checkAdditionalContent ?? DefaultCheckAdditionalContent;
set => _checkAdditionalContent = value;
}
static JsonSerializerSettings()
{
DefaultContext = new StreamingContext();
DefaultCulture = CultureInfo.InvariantCulture;
}
///
/// Initializes a new instance of the class.
///
[DebuggerStepThrough]
public JsonSerializerSettings()
{
Converters = new List();
}
}
}