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
don't rely on GetRawText
  • Loading branch information
manuc66 committed Feb 23, 2020
1 parent 443066f commit 7c9df3e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions NewApi/JsonSubtypes2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,18 @@ private static Type GetElementType(Type arrayOrGenericContainer)

private T ReadObject(ref Utf8JsonReader reader, Type objectType, JsonSerializerOptions serializer)
{
// Copy the current state from reader (it's a struct)
var readerAtStart = reader;

var jObject = JsonDocument.ParseValue(ref reader);

var targetType = GetType(jObject, objectType, serializer) ?? objectType;
var targetType = GetType(jObject, objectType, serializer);
if (targetType is null)
{
throw new JsonException($"Unable to resolve a subtype of {objectType.Name}");
}

return (T)JsonSerializer.Deserialize(jObject.RootElement.GetRawText(), targetType);
return (T)JsonSerializer.Deserialize(ref readerAtStart, targetType, serializer);
}

Type IJsonSubtypes.GetType(JsonDocument jObject, Type parentType)
Expand Down

0 comments on commit 7c9df3e

Please sign in to comment.