Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json of T Buggy JsonConverterOfT #1153

Closed
vintzl opened this issue Dec 25, 2019 · 3 comments
Closed

System.Text.Json of T Buggy JsonConverterOfT #1153

vintzl opened this issue Dec 25, 2019 · 3 comments

Comments

@vintzl
Copy link

vintzl commented Dec 25, 2019

If I try to register many custom converters of T, in ConfigureServices or elsewhere, and I try to use the custom converter with a CanConvert overridden method, I get:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Text.Json.dll: 'The converter 'Buggy_JsonConverterOfT.CustomJsonConverter`1[System.Collections.Generic.List`1[Buggy_JsonConverterOfT.Customer]]' is not compatible with the type 'Buggy_JsonConverterOfT.Customer'.'

Indeed, even if the CanConvert method return true, the JsonConverter yield false

Here a simple (drafty) repro:

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 25, 2019
@ahsonkhan ahsonkhan added this to the 5.0 milestone Feb 20, 2020
@ahsonkhan ahsonkhan added needs more info and removed untriaged New issue has not been triaged by the area owner labels Feb 20, 2020
@ahsonkhan
Copy link
Member

ahsonkhan commented Feb 20, 2020

What are you trying to do here with the "buggy" converter and what do you expect the behavior to be? It is hard to parse what you are trying to do here. Does JsonConverterFactory work for your use case?

Did you try different registration order of the converters?

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to#sample-factory-pattern-converter
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonconverterfactory?view=netcore-3.1

@vintzl
Copy link
Author

vintzl commented Feb 25, 2020

in CanConvert I want to put some logic, also to avoid such issue.

I did try different registration order but I got the same result.

@layomia
Copy link
Contributor

layomia commented Aug 4, 2020

The CanConvert method of your generic custom converter always returns true which means that you're indicating that the first concrete converter you added to JsonSerializerOptions.Converters should be used for all types in the input object graph. This is what causes the exception you are seeing. Specifically, in your repro project, a CustomJsonConverter<List<Customer>> converter which derives from JsonConverter<List<Customer>> cannot be used to convert a Customer type.

Please see the docs on how to follow the factory converter pattern which is ideal for writing generic converters.

@layomia layomia closed this as completed Aug 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants