Lean  $LEAN_TAG$
ScatterChartPointJsonConverter.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 Newtonsoft.Json;
18 using Newtonsoft.Json.Linq;
19 
20 namespace QuantConnect
21 {
22  /// <summary>
23  /// <see cref="ScatterChartPoint"/> json converter
24  /// </summary>
25  public class ScatterChartPointJsonConverter : JsonConverter
26  {
27  /// <summary>
28  /// Default writer
29  /// </summary>
30  public override bool CanWrite => false;
31 
32  /// <summary>
33  /// Determine if this Converter can convert this type
34  /// </summary>
35  /// <param name="objectType">Type that we would like to convert</param>
36  /// <returns>True if <see cref="Series"/></returns>
37  public override bool CanConvert(Type objectType)
38  {
39  return objectType == typeof(ScatterChartPoint);
40  }
41 
42  /// <summary>
43  /// Reads series from Json
44  /// </summary>
45  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
46  {
47  if (reader.TokenType == JsonToken.StartObject)
48  {
49  var jObject = JObject.Load(reader);
50  var tooltip = jObject.TryGetPropertyValue<string>("tooltip");
51 
52  return new ScatterChartPoint(jObject["x"].Value<long>(), jObject["y"].Value<decimal?>(), tooltip);
53  }
54 
55  var jArray = JArray.Load(reader);
56  return new ScatterChartPoint(jArray[0].Value<long>(), jArray[1].Value<decimal?>());
57  }
58 
59  /// <summary>
60  /// Writes a Json from series
61  /// </summary>
62  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
63  {
64  throw new NotImplementedException();
65  }
66  }
67 }