Skip to content

Commit cf73943

Browse files
Add AttributeTargets.Interface to JsonConverterAttribute (#54922)
* Allow JsonConverterAttribute usage on interfaces. Fix #33112 * update ApiCompat baseline
1 parent 430d87f commit cf73943

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

src/libraries/System.Text.Json/ref/System.Text.Json.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public abstract partial class JsonConverter
763763
internal JsonConverter() { }
764764
public abstract bool CanConvert(System.Type typeToConvert);
765765
}
766-
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)]
766+
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)]
767767
public partial class JsonConverterAttribute : System.Text.Json.Serialization.JsonAttribute
768768
{
769769
protected JsonConverterAttribute() { }

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonConverterAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace System.Text.Json.Serialization
1515
/// <see cref="JsonSerializerOptions.Converters"/> or there is another <see cref="JsonConverterAttribute"/> on a property or field
1616
/// of the same type.
1717
/// </remarks>
18-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
18+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
1919
public class JsonConverterAttribute : JsonAttribute
2020
{
2121
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Linq;
5+
using System.Collections.Generic;
6+
using Xunit;
7+
8+
namespace System.Text.Json.Serialization.Tests
9+
{
10+
public static partial class CustomConverterTests
11+
{
12+
[JsonConverter(typeof(MyInterfaceConverter))]
13+
private interface IMyInterface
14+
{
15+
int IntValue { get; set; }
16+
string StringValue { get; set; }
17+
}
18+
19+
// A custom converter that writes and reads the string property as a top-level value
20+
private class MyInterfaceConverter : JsonConverter<IMyInterface>
21+
{
22+
public override IMyInterface Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
23+
=> new MyClass
24+
{
25+
IntValue = 42,
26+
StringValue = reader.GetString()
27+
};
28+
29+
public override void Write(Utf8JsonWriter writer, IMyInterface value, JsonSerializerOptions options) => writer.WriteStringValue(value.StringValue);
30+
}
31+
32+
private class MyClass : IMyInterface
33+
{
34+
public int IntValue { get; set; }
35+
public string StringValue { get; set; }
36+
}
37+
38+
[Fact]
39+
public static void CustomInterfaceConverter_Serialization()
40+
{
41+
IMyInterface value = new MyClass { IntValue = 11, StringValue = "myString" };
42+
43+
string expectedJson = "\"myString\"";
44+
string actualJson = JsonSerializer.Serialize(value);
45+
Assert.Equal(expectedJson, actualJson);
46+
}
47+
48+
[Fact]
49+
public static void CustomInterfaceConverter_Deserialization()
50+
{
51+
string json = "\"myString\"";
52+
53+
IMyInterface result = JsonSerializer.Deserialize<IMyInterface>(json);
54+
55+
Assert.IsType<MyClass>(result);
56+
Assert.Equal("myString", result.StringValue);
57+
Assert.Equal(42, result.IntValue);
58+
}
59+
}
60+
}

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Dynamic.Sample.cs" />
120120
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.HandleNull.cs" />
121121
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Int32.cs" />
122+
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Interface.cs" />
122123
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.InvalidCast.cs" />
123124
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.List.cs" />
124125
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.NullValueType.cs" />

src/libraries/shims/ApiCompatBaseline.PreviousNetCoreApp.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,5 @@ CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatfo
184184
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesGcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
185185
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesCcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
186186
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesGcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
187-
Total Issues: 170
187+
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'System.Text.Json.Serialization.JsonConverterAttribute' changed from '[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple=false)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Interface | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple=false)]' in the implementation.
188+
Total Issues: 171

0 commit comments

Comments
 (0)