18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
33 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
35 var symbol = value as
Symbol;
36 if (ReferenceEquals(symbol,
null))
return;
38 writer.WriteStartObject();
39 writer.WritePropertyName(
"value");
40 writer.WriteValue(symbol.Value);
41 writer.WritePropertyName(
"id");
42 writer.WriteValue(symbol.ID.ToString());
43 writer.WritePropertyName(
"permtick");
44 writer.WriteValue(symbol.Value);
45 if (symbol.HasUnderlying)
47 writer.WritePropertyName(
"underlying");
48 WriteJson(writer, symbol.Underlying, serializer);
50 writer.WriteEndObject();
60 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
62 var jobject = JObject.Load(reader);
64 if (jobject.TryGetValue(
"type", StringComparison.InvariantCultureIgnoreCase, out var type))
66 return BuildSymbolFromUserFriendlyValue(jobject);
68 return ReadSymbolFromJson(jobject);
80 return objectType == typeof (
Symbol);
83 private Symbol ReadSymbolFromJson(JObject jObject)
88 if ((jObject.TryGetValue(
"ID", StringComparison.InvariantCultureIgnoreCase, out symbolId)
89 && jObject.TryGetValue(
"Value", StringComparison.InvariantCultureIgnoreCase, out value))
90 || (jObject.TryGetValue(
"id", StringComparison.InvariantCultureIgnoreCase, out symbolId)
91 && jObject.TryGetValue(
"value", StringComparison.InvariantCultureIgnoreCase, out value)))
93 Symbol underlyingSymbol =
null;
95 if (jObject.TryGetValue(
"Underlying", StringComparison.InvariantCultureIgnoreCase, out underlying)
96 || jObject.TryGetValue(
"underlying", StringComparison.InvariantCultureIgnoreCase, out underlying))
98 underlyingSymbol = ReadSymbolFromJson(underlying as JObject);
101 return new Symbol(SecurityIdentifier.Parse(symbolId.ToString()), value.ToString(), underlyingSymbol);
109 private Symbol BuildSymbolFromUserFriendlyValue(JObject jObject)
111 if (jObject.TryGetValue(
"value", StringComparison.InvariantCultureIgnoreCase, out var value)
112 && jObject.TryGetValue(
"type", StringComparison.InvariantCultureIgnoreCase, out var securityTypeToken)
113 && securityTypeToken.ToString().TryParseSecurityType(out var securityType))
117 return SymbolRepresentation.ParseOptionTickerOSI(value.ToString());
121 return SymbolRepresentation.ParseFutureSymbol(value.ToString());
125 return SymbolRepresentation.ParseFutureOptionSymbol(value.ToString());
129 return SymbolRepresentation.ParseOptionTickerOSI(value.ToString(), securityType: securityType);
132 if (!jObject.TryGetValue(
"market", StringComparison.InvariantCultureIgnoreCase, out var market))
136 return Symbol.Create(value.ToString(), securityType, market.ToString());