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
improvements
  • Loading branch information
manuc66 committed Feb 24, 2020
1 parent a445fc1 commit 29acba3
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion NewApi.Tests/BaseIsAnInterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ConcurrentThreadTest()
public void UnknownMappingFails()
{
var exception = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<IAnimal>("{\"Sound\":\"Scream\"}"));
Assert.AreEqual("Could not create an instance of type JsonSubTypes.Tests.BaseIsAnInterfaceTests+IAnimal. Type is an interface or abstract class and cannot be instantiated. Path 'Sound', line 1, position 9.", exception.Message);
Assert.AreEqual("Could not create an instance of type JsonSubTypes.Tests.BaseIsAnInterfaceTests+IAnimal. Type is an interface or abstract class and cannot be instantiated. Position: 0.", exception.Message);
}
}
}
1 change: 0 additions & 1 deletion NewApi.Tests/DemoAlternativeTypePropertyNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public void DemoCaseInsensitive()

public void DemoBaseWhenNull()
{
Assert.Fail("Not ready");
var animal =
JsonSerializer.Deserialize<Animal>(
"{\"ClassName\": null,\"Color\":\"blue\"}");
Expand Down
1 change: 0 additions & 1 deletion NewApi.Tests/DiscriminatorOfDifferentKindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class Child2 : Child
[Test]
public void DiscriminatorValueCanBeANumber()
{
Assert.Fail("Not ready");
var root1 = JsonSerializer.Deserialize<Parent>("{\"child\":{\"ChildType\":1}}");
var root2 = JsonSerializer.Deserialize<Parent>("{\"child\":{\"ChildType\":2}}");
var root3 = JsonSerializer.Deserialize<Parent>("{\"child\":{\"ChildType\":8}}");
Expand Down
1 change: 0 additions & 1 deletion NewApi.Tests/JsonSubTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ public void DeserializeBadDocument()
[Test]
public void WhenDiscriminatorValueIsNullDeserializeToBaseType()
{
Assert.Fail("Not ready");
var expected = new Root
{
Content = new Base()
Expand Down
4 changes: 0 additions & 4 deletions NewApi.Tests/TypePropertyCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Foo : DtoBase
[Test]
public void FooParsingCamelCase()
{
Assert.Fail("Not ready");
var serializeObject = "{\"MsgType\":1}";
var msgType = JsonSerializer.Deserialize<Foo>(serializeObject).MsgType;
Assert.AreEqual(1, msgType);
Expand Down Expand Up @@ -65,7 +64,6 @@ public void FooParsingCamelCase()
[Test]
public void FooParsingLowerPascalCase()
{
Assert.Fail("Not ready");
var serializeObject = "{\"msgType\":1}";
Assert.AreEqual(1, JsonSerializer.Deserialize<Foo>(serializeObject).MsgType);
Assert.IsInstanceOf<Foo>(JsonSerializer.Deserialize<DtoBase>(serializeObject));
Expand All @@ -89,7 +87,6 @@ class Foo : DtoBase
[Test]
public void FooParsingCamelCase()
{
Assert.Fail("Not ready");
var serializeObject = "{\"MessageType\":1}";
Assert.AreEqual(1, JsonSerializer.Deserialize<Foo>(serializeObject).MsgType);
Assert.IsInstanceOf<Foo>(JsonSerializer.Deserialize<DtoBase>(serializeObject));
Expand Down Expand Up @@ -129,7 +126,6 @@ public void FooParsingCamelCase()
[Test]
public void FooParsingLowerPascalCase()
{
Assert.Fail("Not ready");
var serializeObject = "{\"messageType\":1}";
Assert.AreEqual(1, JsonSerializer.Deserialize<Foo>(serializeObject).MsgType);
Assert.IsInstanceOf<Foo>(JsonSerializer.Deserialize<DtoBase>(serializeObject));
Expand Down
9 changes: 7 additions & 2 deletions NewApi/JsonSubtypes2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private T ReadObject(ref Utf8JsonReader reader, Type objectType, JsonSerializerO
var targetType = GetType(jObject, objectType, serializer);
if (targetType is null)
{
throw new JsonException($"Unable to resolve a subtype of {objectType.Name}");
throw new JsonException($"Could not create an instance of type {objectType.FullName}. Type is an interface or abstract class and cannot be instantiated. Position: {reader.Position.GetInteger()}.");
}

return (T)JsonSerializer.Deserialize(ref readerAtStart, targetType, serializer);
Expand All @@ -203,11 +203,16 @@ private Type GetType(JsonDocument jObject, Type parentType, JsonSerializerOption
Type targetType = parentType;
IJsonSubtypes lastTypeResolver = null;
IJsonSubtypes currentTypeResolver = GetTypeResolver(ToTypeInfo(targetType), serializer.Converters.OfType<IJsonSubtypes>());
var visitedTypes = new HashSet<Type> { targetType };

var jsonConverterCollection = serializer.Converters.OfType<IJsonSubtypes>().ToList();
while (currentTypeResolver != null && currentTypeResolver != lastTypeResolver)
{
targetType = currentTypeResolver.GetType(jObject, targetType);
if (!visitedTypes.Add(targetType))
{
break;
}
lastTypeResolver = currentTypeResolver;
jsonConverterCollection = jsonConverterCollection.Where(c => c != currentTypeResolver).ToList();
currentTypeResolver = GetTypeResolver(ToTypeInfo(targetType), jsonConverterCollection);
Expand All @@ -224,7 +229,7 @@ private IJsonSubtypes GetTypeResolver(TypeInfo targetType, IEnumerable<IJsonSubt
}

var jsonConverterAttribute = GetAttribute<JsonSubTypeConverterAttribute>(targetType);
if (jsonConverterAttribute != null && ToTypeInfo(typeof(JsonSubtypes<T>)).IsAssignableFrom(ToTypeInfo(jsonConverterAttribute.ConverterType)))
if (jsonConverterAttribute != null && ToTypeInfo(typeof(T)).IsAssignableFrom(ToTypeInfo(jsonConverterAttribute.ConverterType.GenericTypeArguments[0])))
{
return (JsonSubtypes<T>)Activator.CreateInstance(jsonConverterAttribute.ConverterType, jsonConverterAttribute.DiscriminatorPropertyName);
}
Expand Down

0 comments on commit 29acba3

Please sign in to comment.