17 using System.Collections.Generic;
19 using Newtonsoft.Json;
20 using Newtonsoft.Json.Linq;
32 private JsonSerializerSettings _settings;
39 _settings =
new JsonSerializerSettings
42 FloatParseHandling = FloatParseHandling.Decimal
53 return objectType.IsAssignableTo(typeof(T));
60 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
62 var token = JToken.ReadFrom(reader);
63 if (token.Type == JTokenType.Null)
68 foreach (JProperty property
in GetProperty(token,
"Charts").Children())
70 foreach (JProperty seriesProperty
in GetProperty(property.Value,
"Series"))
72 var newValues =
new List<JToken>();
73 foreach (var entry
in GetProperty(seriesProperty.Value,
"Values"))
75 if (entry is JObject jobj &&
76 (jobj[
"x"] ==
null || jobj[
"x"].Value<long?>() ==
null ||
77 jobj[
"y"] ==
null || jobj[
"y"].Value<decimal?>() ==
null))
83 if (entry is JArray jArray && jArray.Any(jToken => jToken.Type == JTokenType.Null))
92 var chart = GetProperty(token,
"Charts")[
property.Name];
93 var series = GetProperty(chart,
"Series")[seriesProperty.Name];
94 if (series[
"Values"] !=
null)
96 series[
"Values"] = JArray.FromObject(newValues);
98 else if (series[
"values"] !=
null)
100 series[
"values"] = JArray.FromObject(newValues);
108 return JsonConvert.DeserializeObject<T>(token.ToString(), _settings);
114 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
116 throw new NotImplementedException();
119 private static JToken GetProperty(JToken jToken,
string name)
121 return jToken[name] ?? jToken[name.ToLower()];