From 3876c0470fea56e5a7c7dafd972f950ea6dc6b40 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 20 Apr 2018 17:30:49 -0400 Subject: [PATCH] refactor: Reducing redundant code by moving json serialization/deserialization features into abstract converter --- CHANGELOG.md | 3 + .../Json/AbstractGenericJsonConverter.cs | 75 ++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a11e17..a320b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,4 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added - Adding extension method for getting Informational Version from an Assembly + +### Changed + - JSON serialization/deserialization features moved into base converter \ No newline at end of file diff --git a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs index 151a7ca..ced8956 100644 --- a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs +++ b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs @@ -6,6 +6,15 @@ namespace ICD.Common.Utils.Json { public abstract class AbstractGenericJsonConverter : JsonConverter { + /// + /// Creates a new instance of T. + /// + /// + protected virtual T Instantiate() + { + return ReflectionUtils.CreateInstance(); + } + /// /// Writes the JSON representation of the object. /// @@ -36,7 +45,24 @@ namespace ICD.Common.Utils.Json /// The value. /// The calling serializer. [PublicAPI] - public abstract void WriteJson(JsonWriter writer, T value, JsonSerializer serializer); + public virtual void WriteJson(JsonWriter writer, T value, JsonSerializer serializer) + { + writer.WriteStartObject(); + { + WriteProperties(writer, value, serializer); + } + writer.WriteEndObject(); + } + + /// + /// Override to write properties to the writer. + /// + /// + /// + /// + protected virtual void WriteProperties(JsonWriter writer, T value, JsonSerializer serializer) + { + } /// /// Reads the JSON representation of the object. @@ -70,7 +96,52 @@ namespace ICD.Common.Utils.Json /// The object value. /// [PublicAPI] - public abstract T ReadJson(JsonReader reader, T existingValue, JsonSerializer serializer); + public virtual T ReadJson(JsonReader reader, T existingValue, JsonSerializer serializer) + { + T output = default(T); + bool instantiated = false; + + while (reader.Read()) + { + if (reader.TokenType == JsonToken.Null || reader.TokenType == JsonToken.EndObject) + break; + + if (!instantiated) + { + instantiated = true; + output = Instantiate(); + } + + // Get the property + if (reader.TokenType != JsonToken.PropertyName) + continue; + string property = (string)reader.Value; + + // Read into the value + reader.Read(); + + switch (property) + { + default: + ReadProperty(property, reader, output, serializer); + break; + } + } + + return output; + } + + /// + /// Override to handle the current property value with the given name. + /// + /// + /// + /// + /// + protected virtual void ReadProperty(string property, JsonReader reader, T instance, JsonSerializer serializer) + { + reader.Skip(); + } /// /// Determines whether this instance can convert the specified object type.