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

String enum converter#38702

Merged
JeremyKuhne merged 5 commits intodotnet:masterfrom
JeremyKuhne:enconv
Jun 20, 2019
Merged

String enum converter#38702
JeremyKuhne merged 5 commits intodotnet:masterfrom
JeremyKuhne:enconv

Conversation

@JeremyKuhne
Copy link
Member

Adds public converter for converting enums to strings and vice-versa.

@JamesNK
Copy link
Member

JamesNK commented Jun 19, 2019

Json.NET supports EnumMemberAttribute to customize enum name values. It is a well used feature.

You don't need it in 3.0, but you will get a lot of requests for customizing enum names. Design for adding it in the future.

@JamesNK
Copy link
Member

JamesNK commented Jun 19, 2019

Check out https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Utilities/EnumUtils.cs

This is where Json.NET's enum heavy lifting logic lives. I basically build a contract of what the enum will look like for a given naming strategy.

@JeremyKuhne
Copy link
Member Author

You don't need it in 3.0, but you will get a lot of requests for customizing enum names. Design for adding it in the future.

It shouldn't be a problem to layer in that functionality post 3.0. Didn't want to make it too complicated this time given how late we're getting.

@JeremyKuhne JeremyKuhne self-assigned this Jun 19, 2019
using System.IO;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that uses [JsonConverter] to specify the stringenum converter for a single property, but keeping the default converter as-is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the converter attribute does not work with factories.

public virtual void Write(System.Text.Json.Utf8JsonWriter writer, T value, System.Text.Json.JsonEncodedText propertyName, System.Text.Json.JsonSerializerOptions options) { }
public abstract void Write(System.Text.Json.Utf8JsonWriter writer, T value, System.Text.Json.JsonSerializerOptions options);
}
public sealed class JsonStringEnumConverter : System.Text.Json.Serialization.JsonConverterFactory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record, the alternative here is to expose a single enum converter with a bool "TreatAsString" property. A new custom attribute deriving from [JsonConverter] would also need to be created in order to pass that bool on.

using Xunit;

namespace System.Text.Json.Serialization.Tests
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test failure case that just uses Enum class as a property or return value from root (should fail).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean here. Use System.Enum directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes something like JsonSerializer.Parse<Enum>("1") should fail gracefully (JsonException). I see we actually have a test for that -- so nevermind.

Copy link
Contributor

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - we can address any follow-up later after getting in this very important feature.

@JeremyKuhne
Copy link
Member Author

Almost all of the failures were #38734. One failure was #37695.

@JeremyKuhne JeremyKuhne merged commit 66a1894 into dotnet:master Jun 20, 2019
steveharter pushed a commit to steveharter/dotnet_corefx that referenced this pull request Jun 25, 2019
* String enum converter

Adds public converter for converting enums to strings and vice-versa.

* Address feedback.

* Missing a readonly

* Fix exception and swtich to ConcurrentDictionary

* Merge fixup
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* String enum converter

Adds public converter for converting enums to strings and vice-versa.

* Address feedback.

* Missing a readonly

* Fix exception and swtich to ConcurrentDictionary

* Merge fixup


Commit migrated from dotnet/corefx@66a1894
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.

3 participants