Lean  $LEAN_TAG$
NullStringValueConverter.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 using System;
17 using Newtonsoft.Json;
18 
19 namespace QuantConnect.Util
20 {
21  /// <summary>
22  /// Converts the string "null" into a new instance of T.
23  /// This converter only handles deserialization concerns.
24  /// </summary>
25  /// <typeparam name="T">The output type of the converter</typeparam>
26  public class NullStringValueConverter<T> : JsonConverter
27  where T : new()
28  {
29  /// <summary>
30  /// Writes the JSON representation of the object.
31  /// </summary>
32  /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
33  /// <param name="value">The value.</param>
34  /// <param name="serializer">The calling serializer.</param>
35  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
36  {
37  throw new NotImplementedException();
38  }
39 
40  /// <summary>
41  /// Reads the JSON representation of the object.
42  /// </summary>
43  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
44  /// <param name="objectType">Type of the object.</param>
45  /// <param name="existingValue">The existing value of object being read.</param>
46  /// <param name="serializer">The calling serializer.</param>
47  /// <returns>
48  /// The object value.
49  /// </returns>
50  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
51  {
52  if (reader.TokenType == JsonToken.Null || (reader.TokenType == JsonToken.String && (string)reader.Value == "null"))
53  {
54  return new T();
55  }
56  return serializer.Deserialize<T>(reader);
57  }
58 
59  /// <summary>
60  /// Determines whether this instance can convert the specified object type.
61  /// </summary>
62  /// <param name="objectType">Type of the object.</param>
63  /// <returns>
64  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
65  /// </returns>
66  public override bool CanConvert(Type objectType)
67  {
68  throw new NotImplementedException();
69  }
70  }
71 }