Lean  $LEAN_TAG$
ParameterSetJsonConverter.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 using Newtonsoft.Json.Linq;
21 
22 namespace QuantConnect.Api
23 {
24  /// <summary>
25  /// Json converter for <see cref="ParameterSet"/> which creates a light weight easy to consume serialized version
26  /// </summary>
27  public class ParameterSetJsonConverter : JsonConverter
28  {
29  /// <summary>
30  /// Determines whether this instance can convert the specified object type.
31  /// </summary>
32  /// <param name="objectType">Type of the object.</param>
33  /// <returns>
34  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
35  /// </returns>
36  public override bool CanConvert(Type objectType)
37  {
38  return objectType == typeof(ParameterSet);
39  }
40 
41  /// <summary>
42  /// Writes a JSON object from a Parameter set
43  /// </summary>
44  public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
45  {
46  var parameterSet = value as ParameterSet;
47  if (ReferenceEquals(parameterSet, null)) return;
48 
49  writer.WriteStartObject();
50 
51  if (parameterSet.Value != null)
52  {
53  writer.WritePropertyName("parameterSet");
54  serializer.Serialize(writer, parameterSet.Value);
55  }
56 
57  writer.WriteEndObject();
58  }
59 
60  /// <summary>
61  /// Reads the JSON representation of the object.
62  /// </summary>
63  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
64  /// <param name="objectType">Type of the object.</param>
65  /// <param name="existingValue">The existing value of object being read.</param>
66  /// <param name="serializer">The calling serializer.</param>
67  /// <returns>
68  /// The object value.
69  /// </returns>
70  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
71  {
72  if (reader.TokenType == JsonToken.StartArray)
73  {
74  if (JArray.Load(reader).Count == 0)
75  {
76  return new ParameterSet(-1, new Dictionary<string, string>());
77  }
78  }
79  else if (reader.TokenType == JsonToken.StartObject)
80  {
81  var jObject = JObject.Load(reader);
82 
83  var value = jObject["parameterSet"] ?? jObject;
84 
85  var parameterSet = new ParameterSet(-1, value.ToObject<Dictionary<string, string>>());
86 
87  return parameterSet;
88  }
89 
90  throw new ArgumentException($"Unexpected Tokentype {reader.TokenType}");
91  }
92  }
93 }