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() { }
/// <see cref="JsonSerializerOptions.Converters"/> or there is another <see cref="JsonConverterAttribute"/> on a property
/// of the same type.
/// </remarks>
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = false)]
public class JsonConverterAttribute : JsonAttribute
{
/// <summary>
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<MyCustomEnum>("\"Second\"");
+ Assert.Equal(MyCustomEnum.Second, obj);
+
+ obj = JsonSerializer.Deserialize<MyCustomEnum>("2");
+ Assert.Equal(MyCustomEnum.Second, obj);
+ }
}
}