using System; using ICD.Common.Properties; using Newtonsoft.Json; namespace ICD.Common.Utils.Json { public abstract class AbstractGenericJsonConverter : JsonConverter { /// /// Writes the JSON representation of the object. /// /// The to write to. /// The value. /// The calling serializer. public override sealed void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } WriteJson(writer, (T)value, serializer); } /// /// Writes the JSON representation of the object. /// /// The to write to. /// The value. /// The calling serializer. [PublicAPI] public abstract void WriteJson(JsonWriter writer, T value, JsonSerializer serializer); /// /// Reads the JSON representation of the object. /// /// The to read from. /// Type of the object. /// The existing value of object being read. /// The calling serializer. /// /// The object value. /// public override sealed object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; return ReadJson(reader, (T)existingValue, serializer); } /// /// Reads the JSON representation of the object. /// /// The to read from. /// The existing value of object being read. /// The calling serializer. /// /// The object value. /// [PublicAPI] public abstract T ReadJson(JsonReader reader, T existingValue, JsonSerializer serializer); /// /// Determines whether this instance can convert the specified object type. /// /// Type of the object. /// /// true if this instance can convert the specified object type; otherwise, false. /// public override bool CanConvert(Type objectType) { return objectType == typeof(T); } } }