// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information. using System; using System.IO; using Newtonsoft.Json; namespace Microsoft.AspNet.SignalR.Json { /// /// Default implementation over Json.NET. /// public class JsonNetSerializer : IJsonSerializer { private readonly JsonSerializer _serializer; /// /// Initializes a new instance of the class. /// public JsonNetSerializer() : this(new JsonSerializerSettings()) { } /// /// Initializes a new instance of the class. /// /// The to use when serializing and deserializing. public JsonNetSerializer(JsonSerializerSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } // Just override it anyways (we're saving the user) settings.MaxDepth = 20; _serializer = JsonSerializer.Create(settings); } /// /// Deserializes the JSON to a .NET object. /// /// The JSON to deserialize. /// The of object being deserialized. /// The deserialized object from the JSON string. public object Parse(TextReader reader, Type targetType) { return _serializer.Deserialize(reader, targetType); } /// /// Serializes the specified object to a . /// /// The object to serialize /// The to serialize the object to. public void Serialize(object value, TextWriter writer) { var selfSerializer = value as IJsonWritable; if (selfSerializer != null) { selfSerializer.WriteJson(writer); } else { _serializer.Serialize(writer, value); } } } }