/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace QuantConnect.Util { /// /// Provides a base class for a that serializes a /// an input type as some other output type /// /// The type to be serialized /// The output serialized type public abstract class TypeChangeJsonConverter : JsonConverter { // we use a json serializer which allows using non public default constructor private readonly JsonSerializer _jsonSerializer = new JsonSerializer {ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor}; /// /// True will populate TResult object returned by with json properties /// protected virtual bool PopulateProperties => true; /// /// Reads the JSON representation of the object. /// /// The to read from.Type of the object.The existing value of object being read.The calling serializer. /// /// The object value. /// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // Load token from stream var token = JToken.Load(reader); // Create target object based on token var target = Create(objectType, token); return target; } /// /// Writes the JSON representation of the object. /// /// The to write to.The value.The calling serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // Convert the value into TResult to be serialized var valueToSerialize = Convert((T)value); serializer.Serialize(writer, valueToSerialize); } /// /// Determines whether this instance can convert the specified object type. /// /// Type of the object. /// /// true if this instance can convert the specified object type; otherwise, false. /// public override bool CanConvert(Type objectType) { return typeof(T) == objectType; } /// /// Creates an instance of the un-projected type to be deserialized /// /// The input object type, this is the data held in the token /// The input data to be converted into a T /// A new instance of T that is to be serialized using default rules protected virtual T Create(Type type, JToken token) { // reads the token as an object type if (typeof(TResult).IsClass && typeof(T) != typeof(string)) { return Convert(token.ToObject(_jsonSerializer)); } // reads the token as a value type return Convert(token.Value()); } /// /// Convert the input value to a value to be serialized /// /// The input value to be converted before serialziation /// A new instance of TResult that is to be serialzied protected abstract TResult Convert(T value); /// /// Converts the input value to be deserialized /// /// The deserialized value that needs to be converted to T /// The converted value protected abstract T Convert(TResult value); } }