Conversation
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
|
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. |
|
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. |
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. |
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
| using System.IO; | ||
| using Xunit; | ||
|
|
||
| namespace System.Text.Json.Serialization.Tests |
There was a problem hiding this comment.
Please add a test that uses [JsonConverter] to specify the stringenum converter for a single property, but keeping the default converter as-is.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Outdated
Show resolved
Hide resolved
src/System.Text.Json/src/System/Text/Json/Serialization/Converters/JsonValueConverterEnum.cs
Show resolved
Hide resolved
| using Xunit; | ||
|
|
||
| namespace System.Text.Json.Serialization.Tests | ||
| { |
There was a problem hiding this comment.
Please add a test failure case that just uses Enum class as a property or return value from root (should fail).
There was a problem hiding this comment.
Not sure what you mean here. Use System.Enum directly?
There was a problem hiding this comment.
Yes something like JsonSerializer.Parse<Enum>("1") should fail gracefully (JsonException). I see we actually have a test for that -- so nevermind.
Adds public converter for converting enums to strings and vice-versa.
steveharter
left a comment
There was a problem hiding this comment.
LGTM - we can address any follow-up later after getting in this very important feature.
|
Almost all of the failures were #38734. One failure was #37695. |
* 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
* 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
Adds public converter for converting enums to strings and vice-versa.