Lean  $LEAN_TAG$
ProductJsonConverter.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;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of <see cref="JsonConverter"/> that can deserialize <see cref="Product"/>
25  /// </summary>
26  public class ProductJsonConverter : JsonConverter
27  {
28  private Dictionary<string, ProductType> _productTypeMap = new Dictionary<string, ProductType>()
29  {
30  {"Professional Seats", ProductType.ProfessionalSeats},
31  {"Backtest Node", ProductType.BacktestNode},
32  {"Research Node", ProductType.ResearchNode},
33  {"Live Trading Node", ProductType.LiveNode},
34  {"Support", ProductType.Support},
35  {"Data", ProductType.Data},
36  {"Modules", ProductType.Modules}
37  };
38 
39  /// <summary>
40  /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
41  /// </summary>
42  /// <value>
43  /// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
44  /// </value>
45  public override bool CanWrite => false;
46 
47  /// <summary>
48  /// Determines whether this instance can convert the specified object type.
49  /// </summary>
50  /// <param name="objectType">Type of the object.</param>
51  /// <returns>
52  /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
53  /// </returns>
54  public override bool CanConvert(Type objectType)
55  {
56  return objectType == typeof(Product);
57  }
58 
59  /// <summary>
60  /// Writes the JSON representation of the object.
61  /// </summary>
62  /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param><param name="value">The value.</param><param name="serializer">The calling serializer.</param>
63  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
64  {
65  throw new NotImplementedException("The OrderJsonConverter does not implement a WriteJson method;.");
66  }
67 
68  /// <summary>
69  /// Reads the JSON representation of the object.
70  /// </summary>
71  /// <param name="reader">The <see cref="JsonReader"/> to read from.</param><param name="objectType">Type of the object.</param><param name="existingValue">The existing value of object being read.</param><param name="serializer">The calling serializer.</param>
72  /// <returns>
73  /// The object value.
74  /// </returns>
75  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
76  {
77  var jObject = JObject.Load(reader);
78 
79  var result = CreateProductFromJObject(jObject);
80 
81  return result;
82  }
83 
84  /// <summary>
85  /// Create an order from a simple JObject
86  /// </summary>
87  /// <param name="jObject"></param>
88  /// <returns>Order Object</returns>
89  public Product CreateProductFromJObject(JObject jObject)
90  {
91  if (jObject == null)
92  {
93  return null;
94  }
95 
96  var productTypeName = jObject["name"].Value<string>();
97  if (!_productTypeMap.ContainsKey(productTypeName))
98  {
99  return null;
100  }
101 
102  return new Product
103  {
104  Type = _productTypeMap[productTypeName],
105  Items = jObject["items"].ToObject<List<ProductItem>>()
106  };
107  }
108  }
109 }