From dc18bf41a8c0679565dd13c2528a85632874b25f Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Fri, 26 Jul 2019 12:20:30 -0500 Subject: [PATCH] Allow JsonConverterAttribute to be applied to an Enum (dotnet/corefx#39790) Commit migrated from https://github.com/dotnet/corefx/commit/de61fb206e61f105f657ca01b1244580cc9e1e77 --- .../System.Text.Json/ref/System.Text.Json.cs | 2 +- .../Json/Serialization/JsonConverterAttribute.cs | 2 +- .../tests/Serialization/EnumConverterTests.cs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/ref/System.Text.Json.cs b/src/libraries/System.Text.Json/ref/System.Text.Json.cs index 36a42ce..c9560f4 100644 --- a/src/libraries/System.Text.Json/ref/System.Text.Json.cs +++ b/src/libraries/System.Text.Json/ref/System.Text.Json.cs @@ -459,7 +459,7 @@ namespace System.Text.Json.Serialization internal JsonConverter() { } public abstract bool CanConvert(System.Type typeToConvert); } - [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)] + [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)] public partial class JsonConverterAttribute : System.Text.Json.Serialization.JsonAttribute { protected JsonConverterAttribute() { } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs index fd08c8b..2d0573a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs @@ -14,7 +14,7 @@ namespace System.Text.Json.Serialization /// or there is another on a property /// of the same type. /// - [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = false)] public class JsonConverterAttribute : JsonAttribute { /// diff --git a/src/libraries/System.Text.Json/tests/Serialization/EnumConverterTests.cs b/src/libraries/System.Text.Json/tests/Serialization/EnumConverterTests.cs index 17447fd..3c15d9d 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/EnumConverterTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/EnumConverterTests.cs @@ -165,5 +165,25 @@ namespace System.Text.Json.Serialization.Tests public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => s_stringEnumConverter.CreateConverter(typeToConvert, options); } + + [JsonConverter(typeof(JsonStringEnumConverter))] + private enum MyCustomEnum + { + First = 1, + Second =2 + } + + [Fact] + public void EnumWithConverterAttribute() + { + string json = JsonSerializer.Serialize(MyCustomEnum.Second); + Assert.Equal(@"""Second""", json); + + MyCustomEnum obj = JsonSerializer.Deserialize("\"Second\""); + Assert.Equal(MyCustomEnum.Second, obj); + + obj = JsonSerializer.Deserialize("2"); + Assert.Equal(MyCustomEnum.Second, obj); + } } } -- 2.7.4