Skip to content

Add AttributeTargets.Interface to JsonConverterAttribute #54922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public abstract partial class JsonConverter
internal JsonConverter() { }
public abstract bool CanConvert(System.Type typeToConvert);
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=false)]
[System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Enum | System.AttributeTargets.Field | 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 @@ -15,7 +15,7 @@ namespace System.Text.Json.Serialization
/// <see cref="JsonSerializerOptions.Converters"/> or there is another <see cref="JsonConverterAttribute"/> on a property or field
/// of the same type.
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class JsonConverterAttribute : JsonAttribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using System.Collections.Generic;
using Xunit;

namespace System.Text.Json.Serialization.Tests
{
public static partial class CustomConverterTests
{
[JsonConverter(typeof(MyInterfaceConverter))]
private interface IMyInterface
{
int IntValue { get; set; }
string StringValue { get; set; }
}

// A custom converter that writes and reads the string property as a top-level value
private class MyInterfaceConverter : JsonConverter<IMyInterface>
{
public override IMyInterface Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> new MyClass
{
IntValue = 42,
StringValue = reader.GetString()
};

public override void Write(Utf8JsonWriter writer, IMyInterface value, JsonSerializerOptions options) => writer.WriteStringValue(value.StringValue);
}

private class MyClass : IMyInterface
{
public int IntValue { get; set; }
public string StringValue { get; set; }
}

[Fact]
public static void CustomInterfaceConverter_Serialization()
{
IMyInterface value = new MyClass { IntValue = 11, StringValue = "myString" };

string expectedJson = "\"myString\"";
string actualJson = JsonSerializer.Serialize(value);
Assert.Equal(expectedJson, actualJson);
}

[Fact]
public static void CustomInterfaceConverter_Deserialization()
{
string json = "\"myString\"";

IMyInterface result = JsonSerializer.Deserialize<IMyInterface>(json);

Assert.IsType<MyClass>(result);
Assert.Equal("myString", result.StringValue);
Assert.Equal(42, result.IntValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Dynamic.Sample.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.HandleNull.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Int32.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.Interface.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.InvalidCast.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.List.cs" />
<Compile Include="Serialization\CustomConverterTests\CustomConverterTests.NullValueType.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/shims/ApiCompatBaseline.PreviousNetCoreApp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,5 @@ CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatfo
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesGcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesCcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
CannotChangeAttribute : Attribute 'System.Runtime.Versioning.UnsupportedOSPlatformAttribute' on 'System.Security.Cryptography.AesGcm' changed from '[UnsupportedOSPlatformAttribute("browser")]' in the contract to '[UnsupportedOSPlatformAttribute("browser")]' in the implementation.
Total Issues: 170
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.
Total Issues: 171