Lean  $LEAN_TAG$
SingleValueListConverter.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 System.Collections.Generic;
18 using Newtonsoft.Json;
19 
20 namespace QuantConnect.Util
21 {
22  /// <summary>
23  /// Reads json and always produces a List, even if the input has just an object
24  /// </summary>
25  public class SingleValueListConverter<T> : JsonConverter
26  {
27  /// <summary>
28  /// Writes the JSON representation of the object. If the instance is not a list then it will
29  /// be wrapped in a list
30  /// </summary>
31  /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
32  /// <param name="value">The value.</param>
33  /// <param name="serializer">The calling serializer.</param>
34  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
35  {
36  if (value is T)
37  {
38  value = new List<T> {(T)value};
39  }
40  serializer.Serialize(writer, value);
41  }
42 
43  /// <summary>
44  /// Reads the JSON representation of the object. If the JSON represents a singular instance, it will be returned
45  /// in a list.
46  /// </summary>
47  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
48  /// <param name="objectType">Type of the object.</param>
49  /// <param name="existingValue">The existing value of object being read.</param>
50  /// <param name="serializer">The calling serializer.</param>
51  /// <returns>The object value</returns>
52  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
53  {
54  switch (reader.TokenType)
55  {
56  case JsonToken.String:
57  case JsonToken.StartObject:
58  return new List<T> {serializer.Deserialize<T>(reader)};
59  case JsonToken.StartArray:
60  return serializer.Deserialize<List<T>>(reader);
61  default:
62  throw new ArgumentException("The JsonReader is expected to point at a JsonToken.StartObject or JsonToken.StartArray.");
63  }
64  }
65 
66  /// <summary>
67  /// Determines whether this instance can convert the specified object type.
68  /// </summary>
69  /// <param name="objectType">Type of the object.</param>
70  /// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
71  public override bool CanConvert(Type objectType)
72  {
73  return objectType == typeof(T) || objectType == typeof(List<T>);
74  }
75  }
76 }