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

JsonConverterAttribute cannot be applied to an Enum #30361

Closed
Joelius300 opened this issue Jul 24, 2019 · 5 comments · Fixed by dotnet/corefx#39790
Closed

JsonConverterAttribute cannot be applied to an Enum #30361

Joelius300 opened this issue Jul 24, 2019 · 5 comments · Fixed by dotnet/corefx#39790
Assignees
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@Joelius300
Copy link

Since I'm trying to migrate from Newtonsoft.Json to the new System.Text.Json APIs, I have to be able to use a Enum-string converter.

In json.net you can do this really easily with this attribute on the enum.

[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]

Since there is both a JsonConverterAttribute and a JsonStringEnumConverter class, I thought you would do it like this:

[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]

However after getting a compiler error I quickly realized that the JsonConverterAttribute can't even be used on an Enum because it only has the following three AttributeTargets-flags.

  • AttributeTargets.Class
  • AttributeTargets.Struct
  • AttributeTargets.Property

Are you not supposed to use this attribute and converter like this?
Are there any samples because there I've not found any in the docs?

@ghost
Copy link

ghost commented Jul 24, 2019

I was able to do configure this globally in asp.net core without using attributes like this:

services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

I supose you should be able to do the same passing the same options to your JsonSerializer

@Joelius300
Copy link
Author

Joelius300 commented Jul 24, 2019

I tried that. These options won't get used globally when using JsonSerializer (which I fully understand) but also won't get used when using IJSRuntime (which is very surprising to me).
I need a way of ignoring null values for IJSRuntime which uses System.Text.Json and does not expose any possibilities to define options or customize the serializer. The first thought would be one of the JsonAttributes since these will be respected no matter from where and with what options the serializer gets called.

@steveharter
Copy link
Member

This does look to be an oversight on this scenario. I'll bring up a PR soon on this and review it internally for consideration for 3.0.

cc @JeremyKuhne

@steveharter steveharter changed the title How to use JsonStringEnumConverter JsonConverterAttribute cannot be applied to an Enum Jul 26, 2019
@Joelius300
Copy link
Author

Thank you @steveharter. Will this fix be available in one of the next previews and if so in which one? Or do you also do fixes that will only be released on the 3.0 stable release?

@karelz
Copy link
Member

karelz commented Aug 3, 2019

Fixed in master (5.0) in PR dotnet/corefx#39790 and also in release/3.0 branch (3.0) in PR dotnet/corefx#39812.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants