From 68d1f1033a520bd1f286bd7f18233a28752c14aa Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 21 Dec 2021 16:11:02 -0500 Subject: [PATCH] fix: AbstractGenericJsonConverter handles existing values properly --- .../Extensions/JsonReaderExtensions.cs | 18 ------------------ .../Json/AbstractGenericJsonConverter.cs | 13 +++++++++---- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs index 63b363a..e9868d8 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -32,24 +32,6 @@ namespace ICD.Common.Utils.Extensions #else JsonSerializer.CreateDefault(); #endif - return extends.ReadAsObject(serializer); - } - - /// - /// Reads the current token in the reader and deserializes to the given type. - /// - /// - /// - /// - /// - public static T ReadAsObject([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer) - { - if (extends == null) - throw new ArgumentNullException("extends"); - - if (serializer == null) - throw new ArgumentNullException("serializer"); - return serializer.Deserialize(extends); } diff --git a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs index 834a0af..ae10340 100644 --- a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs +++ b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs @@ -133,23 +133,28 @@ namespace ICD.Common.Utils.Json throw new ArgumentNullException("serializer"); if (reader.TokenType == JsonToken.Null) - return default(T); + return existingValue; if (reader.TokenType != JsonToken.StartObject) throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, reader.TokenType)); - return ReadObject(reader, serializer); + return ReadObject(reader, existingValue, serializer); } /// /// Override to handle deserialization of the current StartObject token. /// /// + /// /// /// - protected virtual T ReadObject(JsonReader reader, JsonSerializer serializer) + protected virtual T ReadObject(JsonReader reader, T existingValue, JsonSerializer serializer) { - T output = Instantiate(); +// ReSharper disable CompareNonConstrainedGenericWithNull +// ReSharper disable ConvertConditionalTernaryToNullCoalescing + T output = existingValue == null ? Instantiate() : existingValue; +// ReSharper restore ConvertConditionalTernaryToNullCoalescing +// ReSharper restore CompareNonConstrainedGenericWithNull reader.ReadObject(serializer, (p, r, s) => ReadProperty(p, r, output, s));