-
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
Support for EnumMemberAttribute in JsonConverterEnum #31081
Comments
/cc @steveharter We should discuss if we want to support this and other attributes from |
The basic scenario here is to provide control of the value names used for enum serialization. DCS and Json.NET use EnumMemberAttribute, XML uses XmlEnumAttribute. |
For anyone blocked by this, here's a NuGet package with a converter (JsonStringEnumMemberConverter) we can use ahead of the 5.0 drop: Macross.Json.Extensions Supports nullable enum types and adds EnumMemberAttribute support. |
|
…correct attribute usage. Fix build issues on some platforms. (dotnet#31081)
…ndling of null strings. (dotnet#31081)
…llible reference type. (dotnet#31081)
Here is the new pull request for this issue: #37113. |
Here is the proposed API to fix this issue. EnumMemberAttribute was considered, but the serializer prefers custom/dedicated attributes. This proposed API takes this into account and proposes a new attribute for this purpose. Note: Pinging @layomia as this person expressed interested in this proposal. Rationale and UsageThis may be a nice-to-have; however, as @mikaelkolkinn states, it is a blocker for migrating off of the Newtonsoft Json serializer/deserializer for some customers. Proposed APIThe proposed attribute would be the following. It would permit the overriding of any JsonNamingPolicy provided in the serializer.
The usage of this attribute would be the following.
The result of each one of these example enum members when serialized will be the following json values: DetailsThe serializer will respect this new attribute on both serialization and deserialization. The serializer will allow any string or This attribute will also override any Note: this API proposal is not a replacement for #31619, but a compliment to it. Open Questions
Pull RequestSee pull request #37113 for an example implementation. Updates |
I have a hunch alot of people right now are working around this issue by using @CodeBlanch recommendation. The nuget package referenced has 500,000 + downloads. |
Ya a lot of people using it and some other features have been added based on issues/PRs opened in the repo behind it. It now supports using JsonPropertyName or EnumMemberAttribute (if you are targeting .NET 5+). Has a fix for #31619. Has a feature for defining a fallback value in case something unknown comes in during de-serialization. Also allows you to specify options based on the enum types being converted, or via an attribute, where the stock one you really only can apply settings globally (1 factory per serializer options). I think the other popular one in the NuGet is the TimeSpan converter. Based on traffic to the blog post about it. |
Noddy work-around for those who need it
...
|
Thanks, I had to modify it a bit to get it working for a scenario where the deserialiser, receives numbers instead of the values.
|
I'm going to close this in favor of #29975. |
Hi, thanks for this!
|
Hi, I wonder if supporting EnumMemberAttribute in JsonConverterEnum is planned and if so what the ETA is?
The text was updated successfully, but these errors were encountered: