Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.Text.Json.Serialization
/// <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>
Expand Down
20 changes: 20 additions & 0 deletions src/System.Text.Json/tests/Serialization/EnumConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyCustomEnum>("\"Second\"");
Assert.Equal(MyCustomEnum.Second, obj);

obj = JsonSerializer.Deserialize<MyCustomEnum>("2");
Assert.Equal(MyCustomEnum.Second, obj);
}
}
}