From cdfdce9eb180537576e41ca751f89567d294733f Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 25 Jul 2019 18:09:51 -0500 Subject: [PATCH] Allow JsonConverterAttribute to be applied to an Enum --- src/System.Text.Json/ref/System.Text.Json.cs | 2 +- .../Serialization/JsonConverterAttribute.cs | 2 +- .../tests/Serialization/EnumConverterTests.cs | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/System.Text.Json/ref/System.Text.Json.cs b/src/System.Text.Json/ref/System.Text.Json.cs index 36a42ce8ba13..c9560f4e005e 100644 --- a/src/System.Text.Json/ref/System.Text.Json.cs +++ b/src/System.Text.Json/ref/System.Text.Json.cs @@ -459,7 +459,7 @@ public abstract partial class JsonConverter 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/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs b/src/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs index fd08c8bdc025..2d0573a809d4 100644 --- a/src/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterAttribute.cs +++ b/src/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/System.Text.Json/tests/Serialization/EnumConverterTests.cs b/src/System.Text.Json/tests/Serialization/EnumConverterTests.cs index 17447fd1b220..3c15d9d0869d 100644 --- a/src/System.Text.Json/tests/Serialization/EnumConverterTests.cs +++ b/src/System.Text.Json/tests/Serialization/EnumConverterTests.cs @@ -165,5 +165,25 @@ public override bool CanConvert(Type typeToConvert) 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); + } } }