Lean  $LEAN_TAG$
OptimizationParameterJsonConverter.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 Newtonsoft.Json;
17 using Newtonsoft.Json.Linq;
18 using System;
19 using System.Reflection;
20 
22 {
23  /// <summary>
24  /// Override <see cref="OptimizationParameter"/> deserialization method.
25  /// Can handle <see cref="OptimizationStepParameter"/> instances
26  /// </summary>
27  public class OptimizationParameterJsonConverter : JsonConverter
28  {
29  /// <summary>
30  /// Writes a JSON object from a OptimizationParameter object
31  /// </summary>
32  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
33  {
34  JObject jo = new JObject();
35  Type type = value.GetType();
36 
37  foreach (PropertyInfo prop in type.GetProperties())
38  {
39  if (prop.CanRead)
40  {
41  var attribute = prop.GetCustomAttribute<JsonPropertyAttribute>();
42  object propVal = prop.GetValue(value, null);
43  if (propVal != null)
44  {
45  jo.Add(attribute.PropertyName ?? prop.Name, JToken.FromObject(propVal, serializer));
46  }
47  }
48  }
49  jo.WriteTo(writer);
50  }
51 
52  /// <summary>
53  /// Creates a Optimization Parameter object from a JSON object
54  /// </summary>
55  public override object ReadJson(
56  JsonReader reader,
57  Type objectType,
58  object existingValue,
59  JsonSerializer serializer
60  )
61  {
62  JObject token = JObject.Load(reader);
63  var parameterName = (token.GetValue("name", StringComparison.OrdinalIgnoreCase) ?? token.GetValue("key", StringComparison.OrdinalIgnoreCase))?.Value<string>();
64  if (string.IsNullOrEmpty(parameterName))
65  {
67  }
68 
69  JToken value;
70  JToken minToken;
71  JToken maxToken;
72  OptimizationParameter optimizationParameter = null;
73  if (token.TryGetValue("min", StringComparison.OrdinalIgnoreCase, out minToken) &&
74  token.TryGetValue("max", StringComparison.OrdinalIgnoreCase, out maxToken))
75  {
76  var stepToken = token.GetValue("step", StringComparison.OrdinalIgnoreCase)?.Value<decimal>();
77  var minStepToken = token.GetValue("minStep", StringComparison.OrdinalIgnoreCase)?.Value<decimal>() ?? token.GetValue("min-step", StringComparison.OrdinalIgnoreCase)?.Value<decimal>();
78  if (stepToken.HasValue)
79  {
80  if (minStepToken.HasValue)
81  {
82  optimizationParameter = new OptimizationStepParameter(parameterName,
83  minToken.Value<decimal>(),
84  maxToken.Value<decimal>(),
85  stepToken.Value,
86  minStepToken.Value);
87  }
88  else
89  {
90  optimizationParameter = new OptimizationStepParameter(parameterName,
91  minToken.Value<decimal>(),
92  maxToken.Value<decimal>(),
93  stepToken.Value);
94  }
95  }
96  else
97  {
98  optimizationParameter = new OptimizationStepParameter(parameterName,
99  minToken.Value<decimal>(),
100  maxToken.Value<decimal>());
101  }
102  }
103  else if(token.TryGetValue("value", StringComparison.OrdinalIgnoreCase, out value))
104  {
105  optimizationParameter = new StaticOptimizationParameter(parameterName, value.Value<string>());
106  }
107 
108  if (optimizationParameter == null)
109  {
111  }
112 
113  return optimizationParameter;
114  }
115 
116  /// <summary>
117  /// Determines if an OptimizationParameter is assignable from the given object type
118  /// </summary>
119  public override bool CanConvert(Type objectType) => typeof(OptimizationParameter).IsAssignableFrom(objectType);
120  }
121 }