diff --git a/NewApi/JsonSubtypes2.cs b/NewApi/JsonSubtypes2.cs index 0fc8b7c..f3366c5 100644 --- a/NewApi/JsonSubtypes2.cs +++ b/NewApi/JsonSubtypes2.cs @@ -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)