Skip to content

Commit

Permalink
Support for System.Text.Json in .NET Core 3 #85
Browse files Browse the repository at this point in the history
First test pass !
  • Loading branch information
manuc66 committed Feb 23, 2020
1 parent 97604d9 commit 3caa4bb
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions NewApi/JsonSubtypes2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -55,7 +72,12 @@ public KnownSubTypeWithPropertyAttribute(Type subType, string propertyName)
}
}

public class JsonSubtypes<T> : JsonConverter<T>
public interface IJsonSubtypes
{
Type GetType(JsonDocument jObject, Type parentType);
bool CanConvert(Type toType);
}
public class JsonSubtypes<T> : JsonConverter<T>, IJsonSubtypes
{

protected readonly string JsonDiscriminatorPropertyName;
Expand Down Expand Up @@ -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)
Expand All @@ -190,10 +212,10 @@ private Type GetType(JsonDocument jObject, Type parentType)
private Type GetType(JsonDocument jObject, Type parentType, JsonSerializerOptions serializer)
{
Type targetType = parentType;
JsonSubtypes<T> lastTypeResolver = null;
JsonSubtypes<T> currentTypeResolver = this;
IJsonSubtypes lastTypeResolver = null;
IJsonSubtypes currentTypeResolver = currentTypeResolver = GetTypeResolver(ToTypeInfo(targetType), serializer.Converters.OfType<IJsonSubtypes>());

var jsonConverterCollection = serializer.Converters.OfType<JsonSubtypes<T>>().ToList();
var jsonConverterCollection = serializer.Converters.OfType<IJsonSubtypes>().ToList();
while (currentTypeResolver != null && currentTypeResolver != lastTypeResolver)
{
targetType = currentTypeResolver.GetType(jObject, targetType);
Expand All @@ -205,7 +227,7 @@ private Type GetType(JsonDocument jObject, Type parentType, JsonSerializerOption
return targetType;
}

private JsonSubtypes<T> GetTypeResolver(TypeInfo targetType, IEnumerable<JsonSubtypes<T>> jsonConverterCollection)
private IJsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable<IJsonSubtypes> jsonConverterCollection)
{
if (targetType == null)
{
Expand Down Expand Up @@ -311,14 +333,13 @@ private static Type GetTypeFromMapping(NullableDictionary<object, Type> 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;
Expand Down

0 comments on commit 3caa4bb

Please sign in to comment.