Skip to content

Commit

Permalink
Allow JsonConverterAttribute usage on interfaces.
Browse files Browse the repository at this point in the history
Fix #33112
  • Loading branch information
eiriktsarpalis committed Jul 12, 2021
1 parent 98b7ed1 commit 7a731f3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
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

0 comments on commit 7a731f3

Please sign in to comment.