Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

EnumMemberAttribute support - #41648

Closed
CodeBlanch wants to merge 1 commit into
dotnet:masterfrom
CodeBlanch:EnumMemberSupport
Closed

CodeBlanch wants to merge 1 commit into
dotnet:masterfrom
CodeBlanch:EnumMemberSupport

Conversation

@CodeBlanch

@CodeBlanch CodeBlanch commented Oct 8, 2019 •

Copy link
Copy Markdown
Contributor
  1. Added support for using EnumMemberAttribute with JsonStringEnumConverter to change the value written/read from JSON when processing enums.

Example:

        [JsonConverter(typeof(JsonStringEnumConverter))]
        public enum MyCustomEnum
        {
            First = 1,
            Second = 2,
            [EnumMember(Value = "third value")]
            Third = 3
        }

In that example MyCustomEnum.Third will round-trip as "third value" in JSON.

This gives System.Text.Json parity with Json.Net wrt Enums.

  1. Fixed JsonNamingPolicy round-trip issue.

If for some reason you define a JsonNamingPolicy that builds values Enum.Parse won't understand, than enums won't round-trip.

Example:

        internal class JsonUpperInvariantWithUnderscoreNamingPolicy : JsonNamingPolicy
        {
            public override string ConvertName(string name) => $"_{name.ToUpperInvariant()}";
        }

That JsonNamingPolicy will create values during serialization that won't be understood during deserialization.

Issues

Resolves https://github.com/dotnet/corefx/issues/41578

…en/read from JSON when processing enums. Fixed JsonNamingPolicy round-trip issue.
@StefH

StefH commented Oct 29, 2019

Copy link
Copy Markdown

I've also created a project + NuGet which extends the JsonStringEnumConverter to supports attributes like EnumMember, Display and Description.

For details, see https://github.com/StefH/System.Text.Json.EnumExtensions

@maryamariyan

maryamariyan commented Nov 6, 2019 •

Copy link
Copy Markdown

Thank you for your contribution. As announced in dotnet/coreclr#27549 this repository will be moving to dotnet/runtime on November 13. If you would like to continue working on this PR after this date, the easiest way to move the change to dotnet/runtime is:

  1. In your corefx repository clone, create patch by running git format-patch origin
  2. In your runtime repository clone, apply the patch by running git apply --directory src/libraries <path to the patch created in step 1>

@ericstj

ericstj commented Nov 12, 2019

Copy link
Copy Markdown
Member

Can we first close on the discussion of wether or not we want this feature before adding the PR? It's unusual for us to support a single attribute from System.Runtime.Serialization without supporting all the extensibility points defined here. I'd like for folks to weigh on that discussion before moving forward with a PR. Thank you for your contribution and let's move discussion over to #41578.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants