17 using System.Reflection;
18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
46 return typeof(
TimeInForce).IsAssignableFrom(objectType);
53 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
56 if (ReferenceEquals(timeInForce,
null))
return;
58 var jo =
new JObject();
60 var type = value.GetType();
64 jo.Add(
"$type", type.FullName);
67 foreach (var property
in type.GetProperties())
71 var propertyValue =
property.GetValue(value,
null);
72 if (propertyValue !=
null)
74 jo.Add(property.Name, JToken.FromObject(propertyValue, serializer));
89 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
91 var jObject = JToken.Load(reader);
94 var array = jObject as JArray;
99 throw new InvalidOperationException($
"Unexpected time in force value: {jObject}");
104 else if (jObject[
"$type"] !=
null)
106 var jToken = jObject[
"$type"];
107 var typeName = jToken.ToString();
108 type = Type.GetType(typeName, throwOnError:
false, ignoreCase:
true);
111 throw new InvalidOperationException($
"Unable to find the type: {typeName}");
120 var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[0],
null);
121 if (constructor ==
null)
123 throw new NotImplementedException($
"Unable to find a constructor for type: {type.FullName}");
126 var timeInForce = constructor.Invoke(
null);
128 foreach (var property
in timeInForce.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
130 var value = jObject[
property.Name];
133 property.SetValue(timeInForce, value.ToObject(property.PropertyType));