From 3caa4bb007b5f41c945596db23bc561b8f610eac Mon Sep 17 00:00:00 2001 From: Emmanuel Counasse Date: Sun, 23 Feb 2020 13:00:19 +0100 Subject: [PATCH] Support for System.Text.Json in .NET Core 3 #85 First test pass ! --- NewApi/JsonSubtypes2.cs | 49 +++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/NewApi/JsonSubtypes2.cs b/NewApi/JsonSubtypes2.cs index 305dfd4..dd6d7cd 100644 --- a/NewApi/JsonSubtypes2.cs +++ b/NewApi/JsonSubtypes2.cs @@ -16,6 +16,23 @@ public JsonSubTypeConverterAttribute(Type converterType, string discriminatorPro { DiscriminatorPropertyName = discriminatorPropertyName; } + + public override bool IsDefaultAttribute() + { + return base.IsDefaultAttribute(); + } + + public override bool Match(object obj) + { + return base.Match(obj); + } + + public override object TypeId { get; } + + public override JsonConverter CreateConverter(Type typeToConvert) + { + return base.CreateConverter(typeToConvert); + } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)] @@ -55,7 +72,12 @@ public KnownSubTypeWithPropertyAttribute(Type subType, string propertyName) } } - public class JsonSubtypes : JsonConverter + public interface IJsonSubtypes + { + Type GetType(JsonDocument jObject, Type parentType); + bool CanConvert(Type toType); + } + public class JsonSubtypes : JsonConverter, IJsonSubtypes { protected readonly string JsonDiscriminatorPropertyName; @@ -172,7 +194,7 @@ private T ReadObject(ref Utf8JsonReader reader, Type objectType, JsonSerializerO return (T)JsonSerializer.Deserialize(jObject.RootElement.GetRawText(), targetType); } - private Type GetType(JsonDocument jObject, Type parentType) + Type IJsonSubtypes.GetType(JsonDocument jObject, Type parentType) { Type resolvedType; if (JsonDiscriminatorPropertyName == null) @@ -190,10 +212,10 @@ private Type GetType(JsonDocument jObject, Type parentType) private Type GetType(JsonDocument jObject, Type parentType, JsonSerializerOptions serializer) { Type targetType = parentType; - JsonSubtypes lastTypeResolver = null; - JsonSubtypes currentTypeResolver = this; + IJsonSubtypes lastTypeResolver = null; + IJsonSubtypes currentTypeResolver = currentTypeResolver = GetTypeResolver(ToTypeInfo(targetType), serializer.Converters.OfType()); - var jsonConverterCollection = serializer.Converters.OfType>().ToList(); + var jsonConverterCollection = serializer.Converters.OfType().ToList(); while (currentTypeResolver != null && currentTypeResolver != lastTypeResolver) { targetType = currentTypeResolver.GetType(jObject, targetType); @@ -205,7 +227,7 @@ private Type GetType(JsonDocument jObject, Type parentType, JsonSerializerOption return targetType; } - private JsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable> jsonConverterCollection) + private IJsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable jsonConverterCollection) { if (targetType == null) { @@ -311,14 +333,13 @@ private static Type GetTypeFromMapping(NullableDictionary typeMapp var key = typeMapping.NotNullKeys().FirstOrDefault(); if (key != null) { - throw new NotImplementedException(); - //var targetLookupValueType = key.GetType(); - //var lookupValue = discriminatorToken.ToObject(targetLookupValueType); - - //if (typeMapping.TryGetValue(lookupValue, out Type targetType)) - //{ - // return targetType; - //} + var targetLookupValueType = key.GetType(); + var lookupValue = JsonSerializer.Deserialize(discriminatorToken.GetRawText(), targetLookupValueType); + + if (typeMapping.TryGetValue(lookupValue, out Type targetType)) + { + return targetType; + } } return null;