-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
JsonSerializer throw an InvalidCastException when serializing an array of item with a custom converter #46522
Comments
The exception message tells the problem: you must use A |
The way S.T.Json handle converters seems inconsistent:
Maybe S.T.Json should throw an |
It surprises me. I'd expect it not to work. |
You can run the code from the issue and check the result of the first serialization |
If anything, it is likely accidental that the first line happens to work. At the same time, the exception thrown in the second line is not very user friendly. We should consider adding checks that detect incompatible converter types early on and surface a relevant error message. |
This is related to #55135. |
See #55135 (comment) for analysis of the issue and proposals on how to address this in the future. |
Description
JsonSerializer throw an InvalidCastException when serialization a collection of items. The class has a custom JsonConverter that matches one of the interface implemented by the class.
Serializing an
SampleRepro[]
(SampleRepro
implements the interface) throw an InvalidCastException.Configuration
.NET 5.0.1
Regression?
This is new code, I haven't tested with previous versions of System.Text.Json
Other information
I managed to make it work using a
JsonConverterFactory
but I'm not sure this is needed or just a temporary workaround.The text was updated successfully, but these errors were encountered: