-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: EnumConverter support for numeric/string in but always numeric out #61726
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging owners as I wasn't specific enough for the bot to pull the area out, sorry. |
Have you considered using JsonStringEnumConverter? Note that the converter doesn't support reading quoted numeric values, this is already tracked #58247. At this point you'd need a custom converter to cover your use case I'm afraid. |
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and motivationRight now with EnumConverterOptions it's easy enough to say "allow strings" and have i.e. Allow strings is checked first in serialization method and outputs string before falling back to numeric. Lines 154 to 158 in 10e107d
In our transition from Newtonsoft.Json, we found a couple of places where our clients were passing in a string, in one case even the numeric representation as a string (straight from a radio button value) so we want to accept that still, but we always want to serialize and send out the numeric representation as we were before. Right now I need to copy-paste-modify the standard factory and converter to achieve this, but I'd love it if this was support natively via some form of options. API ProposalI imagine this would take shape in some modification to API UsageReplace the standard converter with a custom one as usual. Alternative DesignsNo response RisksCan't think of any. Perf would be same as current string in support.
|
Hi @eiriktsarpalis , I looked at runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs Lines 39 to 45 in d9afc1e
The bug you mentioned would be a blocker for us, but I believe even if that's solved the result of Lines 154 to 158 in 10e107d
Workaround I'll leave it in your court as to whether there's need for what I'm asking as an option flag. I think it'd be useful, but sure you've all got more important things to get on with :) |
In that case, I'm going to close this in favor of #58247. |
To be clear, #58247 isn't a fix for this. My custom converter is the only way. |
IIRC the primary blocker even if #58247 were fixed is that you'd need to serialize as numbers? Yeah, there are all sorts of permutations wrt serialization vs deserialization behavior that the default converter won't be supporting. Recommend using a custom converter to solve your use case. |
Background and motivation
Right now with EnumConverterOptions it's easy enough to say "allow strings" and have
"0"
or"MemberName"
be deserialized, but by opting in to that setting it also affects all the serialization and everything is serialized as the member name string representation.i.e. Allow strings is checked first in serialization method and outputs string before falling back to numeric.
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Lines 154 to 158 in 10e107d
In our transition from Newtonsoft.Json, we found a couple of places where our clients were passing in a string, in one case even the numeric representation as a string (straight from a radio button value) so we want to accept that still, but we always want to serialize and send out the numeric representation as we were before.
Right now I need to copy-paste-modify the standard factory and converter to achieve this, but I'd love it if this was support natively via some form of options.
API Proposal
I imagine this would take shape in some modification to
System.Text.Json.Serialization.Converters.EnumConverterOptions
, but exactly what I'm not sure.API Usage
Replace the standard converter with a custom one as usual.
Alternative Designs
No response
Risks
Can't think of any. Perf would be same as current string in support.
The text was updated successfully, but these errors were encountered: