mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
fix: AbstractGenericJsonConverter handles existing values properly
This commit is contained in:
@@ -32,24 +32,6 @@ namespace ICD.Common.Utils.Extensions
|
||||
#else
|
||||
JsonSerializer.CreateDefault();
|
||||
#endif
|
||||
return extends.ReadAsObject<T>(serializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the current token in the reader and deserializes to the given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="serializer"></param>
|
||||
/// <returns></returns>
|
||||
public static T ReadAsObject<T>([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (serializer == null)
|
||||
throw new ArgumentNullException("serializer");
|
||||
|
||||
return serializer.Deserialize<T>(extends);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override to handle deserialization of the current StartObject token.
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="existingValue"></param>
|
||||
/// <param name="serializer"></param>
|
||||
/// <returns></returns>
|
||||
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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user