Lean  $LEAN_TAG$
JsonRoundingConverter.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 
20 namespace QuantConnect.Util
21 {
22  /// <summary>
23  /// Helper <see cref="JsonConverter"/> that will round decimal and double types,
24  /// to <see cref="FractionalDigits"/> fractional digits
25  /// </summary>
26  public class JsonRoundingConverter : JsonConverter
27  {
28  /// <summary>
29  /// The number of fractional digits to round to
30  /// </summary>
31  public const int FractionalDigits = 4;
32 
33  /// <summary>
34  /// Will always return false.
35  /// Gets a value indicating whether this <see cref="T:Newtonsoft.Json.JsonConverter" /> can read JSON.
36  /// </summary>
37  public override bool CanRead => false;
38 
39  /// <summary>
40  /// Determines whether this instance can convert the specified object type.
41  /// </summary>
42  /// <param name="objectType">Type of the object.</param>
43  /// <returns>True if this instance can convert the specified object type</returns>
44  public override bool CanConvert(Type objectType)
45  {
46  return objectType == typeof(decimal)
47  || objectType == typeof(double);
48  }
49 
50  /// <summary>
51  /// Not implemented, will throw <see cref="NotImplementedException"/>
52  /// </summary>
53  /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
54  /// <param name="objectType">Type of the object.</param>
55  /// <param name="existingValue">The existing value of object being read.</param>
56  /// <param name="serializer">The calling serializer.</param>
57  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
58  {
59  throw new NotImplementedException();
60  }
61 
62  /// <summary>
63  /// Writes the JSON representation of the object.
64  /// </summary>
65  /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
66  /// <param name="value">The value.</param>
67  /// <param name="serializer">The calling serializer.</param>
68  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
69  {
70  if (value is double)
71  {
72  var rounded = Math.Round((double)value, FractionalDigits);
73  writer.WriteValue(rounded.ToString(CultureInfo.InvariantCulture));
74  }
75  else
76  {
77  // we serialize decimal as string so that json doesn't use exponential notation which actually will lose precision
78  var rounded = Math.Round((decimal)value, FractionalDigits);
79  writer.WriteValue(rounded.ToString(CultureInfo.InvariantCulture));
80  }
81  }
82  }
83 }