Lean  $LEAN_TAG$
OrderTypeNormalizingJsonConverter.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.Globalization;
18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
20 using QuantConnect.Orders;
21 
22 namespace QuantConnect.Report
23 {
24  /// <summary>
25  /// Normalizes the "Type" field to a value that will allow for
26  /// successful deserialization in the <see cref="OrderJsonConverter"/> class.
27  /// </summary>
28  /// <example>
29  /// All of these values should result in the same object:
30  /// <code>
31  /// [
32  /// { "Type": "marketOnOpen", ... },
33  /// { "Type": "MarketOnOpen", ... },
34  /// { "Type": 4, ... },
35  /// ]
36  /// </code>
37  /// </example>
38  public class OrderTypeNormalizingJsonConverter : JsonConverter
39  {
40  /// <summary>
41  /// Determine if this Converter can convert a given object type
42  /// </summary>
43  /// <param name="objectType">Object type to convert</param>
44  /// <returns>True if assignable from <see cref="Order"/></returns>
45  public override bool CanConvert(Type objectType)
46  {
47  return typeof(Order).IsAssignableFrom(objectType);
48  }
49 
50  /// <summary>
51  /// Read Json and convert
52  /// </summary>
53  /// <returns>Resulting <see cref="Order"/></returns>
54  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
55  {
56  var token = JToken.ReadFrom(reader);
57  var jtokenType = token["Type"] ?? token["type"];
58  int orderType = GetOrderType(jtokenType);
59  if (token["Type"] != null)
60  {
61  token["Type"] = orderType;
62  }
63  else if (token["type"] != null)
64  {
65  token["type"] = orderType;
66  }
67 
68  return OrderJsonConverter.CreateOrderFromJObject((JObject)token);
69  }
70 
71  /// <summary>
72  /// Write Json; Not implemented
73  /// </summary>
74  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
75  {
76  throw new NotImplementedException();
77  }
78 
79  private int GetOrderType(JToken type)
80  {
81  var orderTypeValue = type.Value<string>();
82  int orderTypeNumber;
83  return Parse.TryParse(orderTypeValue, NumberStyles.Any, out orderTypeNumber) ?
84  orderTypeNumber :
85  (int)(OrderType)Enum.Parse(typeof(OrderType), orderTypeValue, true);
86  }
87  }
88 }