Replies: 1 comment 3 replies
-
The answer is in the exception message really. Polymorphic deserialization requires requires using the built-in converters that are capable of reading polymorphism metadata, and in your case being able to resume deserialization after the base converter has read metadata. By swapping out the built-in converter with a custom one your type loses the ability to resume deserialization. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue summary
The polymorphic JSON deserialization in System.Text.Json has some limitations when combined with custom JSON converters. This is documented here:
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism?pivots=dotnet-7-0#additional-polymorphic-serialization-details
I'm creating an issue here because I can't see any technical limitations of why this shouldn't be possible. Supporting this shouldn't require more read-ahead or buffering than not supporting it. Supporting such use cases would be extremely useful for a scenario I have right now where I need a custom JSON converter for doing the type-discrimitation instead of relying on the build-in feature for this, because one of the derived types has a custom JSON converter.
I also assume this causes similar issues with JsonConverter's generated by source-generators. Which basically makes the built-in type-discrimination not combinable with the JSON source generators.
Example code
Throws:
The exception doesn't give a good indication of what the problem is, so it took some time searching the .NET source-code to find the actual cause the exception. Originally thrown from here:
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverter.MetadataHandling.cs
Line 38 in 41772ba
Beta Was this translation helpful? Give feedback.
All reactions