18 using Newtonsoft.Json;
19 using Newtonsoft.Json.Linq;
32 private readonly JsonSerializer _jsonSerializer =
new JsonSerializer {ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor};
46 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
49 var token = JToken.Load(reader);
52 var target =
Create(objectType, token);
61 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
64 var valueToSerialize =
Convert((T)value);
66 serializer.Serialize(writer, valueToSerialize);
78 return typeof(T) == objectType;
87 protected virtual T
Create(Type type, JToken token)
90 if (typeof(TResult).IsClass && typeof(T) != typeof(
string))
92 return Convert(token.ToObject<TResult>(_jsonSerializer));
96 return Convert(token.Value<TResult>());
104 protected abstract TResult
Convert(T value);
111 protected abstract T
Convert(TResult value);