Lean  $LEAN_TAG$
TypeChangeJsonConverter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
20 
21 namespace QuantConnect.Util
22 {
23  /// <summary>
24  /// Provides a base class for a <see cref="JsonConverter"/> that serializes a
25  /// an input type as some other output type
26  /// </summary>
27  /// <typeparam name="T">The type to be serialized</typeparam>
28  /// <typeparam name="TResult">The output serialized type</typeparam>
29  public abstract class TypeChangeJsonConverter<T, TResult> : JsonConverter
30  {
31  // we use a json serializer which allows using non public default constructor
32  private readonly JsonSerializer _jsonSerializer = new JsonSerializer {ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor};
33 
34  /// <summary>
35  /// True will populate TResult object returned by <see cref="Convert(TResult)"/> with json properties
36  /// </summary>
37  protected virtual bool PopulateProperties => true;
38 
39  /// <summary>
40  /// Reads the JSON representation of the object.
41  /// </summary>
42  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param><param name="objectType">Type of the object.</param><param name="existingValue">The existing value of object being read.</param><param name="serializer">The calling serializer.</param>
43  /// <returns>
44  /// The object value.
45  /// </returns>
46  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
47  {
48  // Load token from stream
49  var token = JToken.Load(reader);
50 
51  // Create target object based on token
52  var target = Create(objectType, token);
53 
54  return target;
55  }
56 
57  /// <summary>
58  /// Writes the JSON representation of the object.
59  /// </summary>
60  /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param><param name="value">The value.</param><param name="serializer">The calling serializer.</param>
61  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
62  {
63  // Convert the value into TResult to be serialized
64  var valueToSerialize = Convert((T)value);
65 
66  serializer.Serialize(writer, valueToSerialize);
67  }
68 
69  /// <summary>
70  /// Determines whether this instance can convert the specified object type.
71  /// </summary>
72  /// <param name="objectType">Type of the object.</param>
73  /// <returns>
74  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
75  /// </returns>
76  public override bool CanConvert(Type objectType)
77  {
78  return typeof(T) == objectType;
79  }
80 
81  /// <summary>
82  /// Creates an instance of the un-projected type to be deserialized
83  /// </summary>
84  /// <param name="type">The input object type, this is the data held in the token</param>
85  /// <param name="token">The input data to be converted into a T</param>
86  /// <returns>A new instance of T that is to be serialized using default rules</returns>
87  protected virtual T Create(Type type, JToken token)
88  {
89  // reads the token as an object type
90  if (typeof(TResult).IsClass && typeof(T) != typeof(string))
91  {
92  return Convert(token.ToObject<TResult>(_jsonSerializer));
93  }
94 
95  // reads the token as a value type
96  return Convert(token.Value<TResult>());
97  }
98 
99  /// <summary>
100  /// Convert the input value to a value to be serialized
101  /// </summary>
102  /// <param name="value">The input value to be converted before serialziation</param>
103  /// <returns>A new instance of TResult that is to be serialzied</returns>
104  protected abstract TResult Convert(T value);
105 
106  /// <summary>
107  /// Converts the input value to be deserialized
108  /// </summary>
109  /// <param name="value">The deserialized value that needs to be converted to T</param>
110  /// <returns>The converted value</returns>
111  protected abstract T Convert(TResult value);
112  }
113 }