#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.Generic; using System.IO; using System.Runtime.Serialization.Formatters; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Utilities; using System.Runtime.Serialization; using ErrorEventArgs=Newtonsoft.Json.Serialization.ErrorEventArgs; namespace Newtonsoft.Json { /// /// Serializes and deserializes objects into and from the JSON format. /// The enables you to control how objects are encoded into JSON. /// public class JsonSerializer { #region Properties private TypeNameHandling _typeNameHandling; private FormatterAssemblyStyle _typeNameAssemblyFormat; private PreserveReferencesHandling _preserveReferencesHandling; private ReferenceLoopHandling _referenceLoopHandling; private MissingMemberHandling _missingMemberHandling; private ObjectCreationHandling _objectCreationHandling; private NullValueHandling _nullValueHandling; private DefaultValueHandling _defaultValueHandling; private ConstructorHandling _constructorHandling; private JsonConverterCollection _converters; private IContractResolver _contractResolver; private IReferenceResolver _referenceResolver; private SerializationBinder _binder; private StreamingContext _context; /// /// Occurs when the errors during serialization and deserialization. /// public virtual event EventHandler Error; /// /// Gets or sets the used by the serializer when resolving references. /// public virtual IReferenceResolver ReferenceResolver { get { if (_referenceResolver == null) _referenceResolver = new DefaultReferenceResolver(); return _referenceResolver; } set { if (value == null) throw new ArgumentNullException("value", "Reference resolver cannot be null."); _referenceResolver = value; } } /// /// Gets or sets the used by the serializer when resolving type names. /// public virtual SerializationBinder Binder { get { return _binder; } set { if (value == null) throw new ArgumentNullException("value", "Serialization binder cannot be null."); _binder = value; } } /// /// Gets or sets how type name writing and reading is handled by the serializer. /// public virtual TypeNameHandling TypeNameHandling { get { return _typeNameHandling; } set { if (value < TypeNameHandling.None || value > TypeNameHandling.All) throw new ArgumentOutOfRangeException("value"); _typeNameHandling = value; } } /// /// Gets or sets how a type name assembly is written and resolved by the serializer. /// /// The type name assembly format. public virtual FormatterAssemblyStyle TypeNameAssemblyFormat { get { return _typeNameAssemblyFormat; } set { if (value < FormatterAssemblyStyle.Simple || value > FormatterAssemblyStyle.Full) throw new ArgumentOutOfRangeException("value"); _typeNameAssemblyFormat = value; } } /// /// Gets or sets how object references are preserved by the serializer. /// public virtual PreserveReferencesHandling PreserveReferencesHandling { get { return _preserveReferencesHandling; } set { if (value < PreserveReferencesHandling.None || value > PreserveReferencesHandling.All) throw new ArgumentOutOfRangeException("value"); _preserveReferencesHandling = value; } } /// /// Get or set how reference loops (e.g. a class referencing itself) is handled. /// public virtual ReferenceLoopHandling ReferenceLoopHandling { get { return _referenceLoopHandling; } set { if (value < ReferenceLoopHandling.Error || value > ReferenceLoopHandling.Serialize) throw new ArgumentOutOfRangeException("value"); _referenceLoopHandling = value; } } /// /// Get or set how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization. /// public virtual MissingMemberHandling MissingMemberHandling { get { return _missingMemberHandling; } set { if (value < MissingMemberHandling.Ignore || value > MissingMemberHandling.Error) throw new ArgumentOutOfRangeException("value"); _missingMemberHandling = value; } } /// /// Get or set how null values are handled during serialization and deserialization. /// public virtual NullValueHandling NullValueHandling { get { return _nullValueHandling; } set { if (value < NullValueHandling.Include || value > NullValueHandling.Ignore) throw new ArgumentOutOfRangeException("value"); _nullValueHandling = value; } } /// /// Get or set how null default are handled during serialization and deserialization. /// public virtual DefaultValueHandling DefaultValueHandling { get { return _defaultValueHandling; } set { if (value < DefaultValueHandling.Include || value > DefaultValueHandling.Ignore) throw new ArgumentOutOfRangeException("value"); _defaultValueHandling = value; } } /// /// Gets or sets how objects are created during deserialization. /// /// The object creation handling. public virtual ObjectCreationHandling ObjectCreationHandling { get { return _objectCreationHandling; } set { if (value < ObjectCreationHandling.Auto || value > ObjectCreationHandling.Replace) throw new ArgumentOutOfRangeException("value"); _objectCreationHandling = value; } } /// /// Gets or sets how constructors are used during deserialization. /// /// The constructor handling. public virtual ConstructorHandling ConstructorHandling { get { return _constructorHandling; } set { if (value < ConstructorHandling.Default || value > ConstructorHandling.AllowNonPublicDefaultConstructor) throw new ArgumentOutOfRangeException("value"); _constructorHandling = value; } } /// /// Gets a collection that will be used during serialization. /// /// Collection that will be used during serialization. public virtual JsonConverterCollection Converters { get { if (_converters == null) _converters = new JsonConverterCollection(); return _converters; } } /// /// Gets or sets the contract resolver used by the serializer when /// serializing .NET objects to JSON and vice versa. /// public virtual IContractResolver ContractResolver { get { if (_contractResolver == null) _contractResolver = DefaultContractResolver.Instance; return _contractResolver; } set { _contractResolver = value; } } /// /// Gets or sets the used by the serializer when invoking serialization callback methods. /// /// The context. public virtual StreamingContext Context { get { return _context; } set { _context = value; } } #endregion /// /// Initializes a new instance of the class. /// public JsonSerializer() { _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling; _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling; _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling; _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling; _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling; _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling; _constructorHandling = JsonSerializerSettings.DefaultConstructorHandling; _typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling; _context = JsonSerializerSettings.DefaultContext; _binder = DefaultSerializationBinder.Instance; } /// /// Creates a new instance using the specified . /// /// The settings to be applied to the . /// A new instance using the specified . public static JsonSerializer Create(JsonSerializerSettings settings) { JsonSerializer jsonSerializer = new JsonSerializer(); if (settings != null) { if (!CollectionUtils.IsNullOrEmpty(settings.Converters)) jsonSerializer.Converters.AddRange(settings.Converters); jsonSerializer.TypeNameHandling = settings.TypeNameHandling; jsonSerializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat; jsonSerializer.PreserveReferencesHandling = settings.PreserveReferencesHandling; jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling; jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling; jsonSerializer.ObjectCreationHandling = settings.ObjectCreationHandling; jsonSerializer.NullValueHandling = settings.NullValueHandling; jsonSerializer.DefaultValueHandling = settings.DefaultValueHandling; jsonSerializer.ConstructorHandling = settings.ConstructorHandling; jsonSerializer.Context = settings.Context; if (settings.Error != null) jsonSerializer.Error += settings.Error; if (settings.ContractResolver != null) jsonSerializer.ContractResolver = settings.ContractResolver; if (settings.ReferenceResolver != null) jsonSerializer.ReferenceResolver = settings.ReferenceResolver; if (settings.Binder != null) jsonSerializer.Binder = settings.Binder; } return jsonSerializer; } /// /// Populates the JSON values onto the target object. /// /// The that contains the JSON structure to reader values from. /// The target object to populate values onto. public void Populate(TextReader reader, object target) { Populate(new JsonTextReader(reader), target); } /// /// Populates the JSON values onto the target object. /// /// The that contains the JSON structure to reader values from. /// The target object to populate values onto. public void Populate(JsonReader reader, object target) { PopulateInternal(reader, target); } internal virtual void PopulateInternal(JsonReader reader, object target) { ValidationUtils.ArgumentNotNull(reader, "reader"); ValidationUtils.ArgumentNotNull(target, "target"); JsonSerializerInternalReader serializerReader = new JsonSerializerInternalReader(this); serializerReader.Populate(reader, target); } /// /// Deserializes the Json structure contained by the specified . /// /// The that contains the JSON structure to deserialize. /// The being deserialized. public object Deserialize(JsonReader reader) { return Deserialize(reader, null); } /// /// Deserializes the Json structure contained by the specified /// into an instance of the specified type. /// /// The containing the object. /// The of object being deserialized. /// The instance of being deserialized. public object Deserialize(TextReader reader, Type objectType) { return Deserialize(new JsonTextReader(reader), objectType); } /// /// Deserializes the Json structure contained by the specified /// into an instance of the specified type. /// /// The containing the object. /// The type of the object to deserialize. /// The instance of being deserialized. public T Deserialize(JsonReader reader) { return (T)Deserialize(reader, typeof(T)); } /// /// Deserializes the Json structure contained by the specified /// into an instance of the specified type. /// /// The containing the object. /// The of object being deserialized. /// The instance of being deserialized. public object Deserialize(JsonReader reader, Type objectType) { return DeserializeInternal(reader, objectType); } internal virtual object DeserializeInternal(JsonReader reader, Type objectType) { ValidationUtils.ArgumentNotNull(reader, "reader"); JsonSerializerInternalReader serializerReader = new JsonSerializerInternalReader(this); return serializerReader.Deserialize(reader, objectType); } /// /// Serializes the specified and writes the Json structure /// to a Stream using the specified . /// /// The used to write the Json structure. /// The to serialize. public void Serialize(TextWriter textWriter, object value) { Serialize(new JsonTextWriter(textWriter), value); } /// /// Serializes the specified and writes the Json structure /// to a Stream using the specified . /// /// The used to write the Json structure. /// The to serialize. public void Serialize(JsonWriter jsonWriter, object value) { SerializeInternal(jsonWriter, value); } internal virtual void SerializeInternal(JsonWriter jsonWriter, object value) { ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter"); JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this); serializerWriter.Serialize(jsonWriter, value); } internal JsonConverter GetMatchingConverter(Type type) { return GetMatchingConverter(_converters, type); } internal static JsonConverter GetMatchingConverter(IList converters, Type objectType) { ValidationUtils.ArgumentNotNull(objectType, "objectType"); if (converters != null) { for (int i = 0; i < converters.Count; i++) { JsonConverter converter = converters[i]; if (converter.CanConvert(objectType)) return converter; } } return null; } internal void OnError(ErrorEventArgs e) { EventHandler error = Error; if (error != null) error(this, e); } } }