-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow JsonConverterAttribute usage on interfaces.
Fix #33112
- Loading branch information
1 parent
98b7ed1
commit 7a731f3
Showing
4 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...stem.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.Interface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters