From 05aff7835db2b570ad633ab2f74c6c46d32bce0c Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 4 Oct 2026 12:40:50 +0200 Subject: [PATCH] Custom JsonConverter: move to .NET 10 and add registration tests - Retarget both projects from net7.0 to net10.0. - Newtonsoft.Json 13.0.3 to 13.0.4. - Test packages: Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.1.0. - Remove the unused Microsoft.AspNetCore.Mvc.Testing reference. - Drop the CanWrite and CanRead overrides from SmartContactConverter; both repeat the base class defaults. - Add ConverterRegistrationUnitTest: a converter registered through JsonSerializerSettings.Converters, and a type carrying the JsonConverter attribute. --- .../ConverterRegistrationUnitTest.cs | 42 +++++++++++++++++++ .../CustomJsonConverter.Tests.csproj | 13 +++--- .../Converters/SmartContactConverter.cs | 4 -- .../CustomJsonConverter.csproj | 4 +- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/ConverterRegistrationUnitTest.cs diff --git a/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/ConverterRegistrationUnitTest.cs b/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/ConverterRegistrationUnitTest.cs new file mode 100644 index 0000000000..765d64e140 --- /dev/null +++ b/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/ConverterRegistrationUnitTest.cs @@ -0,0 +1,42 @@ +using CustomJsonConverter.Converters; +using Newtonsoft.Json; +using Xunit; + +namespace CustomJsonConverter.Tests; + +[JsonConverter(typeof(ImprovedContactConverter))] +public record class TaggedContact(string Name, Department Department, string Phone, Address Address) + : Contact(Name, Department, Phone, Address); + +public class ConverterRegistrationUnitTest +{ + [Fact] + public void GivenListOfContacts_WhenConverterIsRegisteredInSettings_ThenWritesCustomizedOutput() + { + var contacts = DataSource.GetContacts(); + var settings = new JsonSerializerSettings + { + Converters = { new SmartContactConverter() } + }; + + var json = JsonConvert.SerializeObject(contacts, settings); + + Assert.Equal(@"[{""Name"":""John"",""Department"":""Admin"",""Address"":{""Street"":""Street 1"",""City"":""City 1""}},{""Name"":""Jane"",""Department"":""CustomerCare"",""Phone"":""+2341"",""Address"":{""Street"":""Street 2"",""City"":""City 2""}},{""Name"":""Mike"",""Department"":""Operations"",""Address"":{""Street"":""Street 3"",""City"":""City 3""}}]", json); + } + + [Fact] + public void GivenTypeWithConverterAttribute_WhenRoundTripped_ThenConverterWritesAndDefaultContractReads() + { + var contact = new TaggedContact("John", Department.Admin, "+1234", new("Street 1", "City 1")); + + var json = JsonConvert.SerializeObject(contact); + + Assert.Equal(@"{""Name"":""John"",""Department"":1}", json); + + var restored = JsonConvert.DeserializeObject(json)!; + + Assert.Equal("John", restored.Name); + Assert.Equal(Department.Admin, restored.Department); + Assert.Null(restored.Phone); + } +} diff --git a/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/CustomJsonConverter.Tests.csproj b/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/CustomJsonConverter.Tests.csproj index ed22e9cd8c..7d92581b96 100644 --- a/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/CustomJsonConverter.Tests.csproj +++ b/json-csharp/CustomJsonConverter/CustomJsonConverter.Tests/CustomJsonConverter.Tests.csproj @@ -1,21 +1,20 @@  - net7.0 + net10.0 enable false - - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/json-csharp/CustomJsonConverter/CustomJsonConverter/Converters/SmartContactConverter.cs b/json-csharp/CustomJsonConverter/CustomJsonConverter/Converters/SmartContactConverter.cs index 8d2b9f1067..f38295fab5 100644 --- a/json-csharp/CustomJsonConverter/CustomJsonConverter/Converters/SmartContactConverter.cs +++ b/json-csharp/CustomJsonConverter/CustomJsonConverter/Converters/SmartContactConverter.cs @@ -41,8 +41,4 @@ public override void WriteJson(JsonWriter writer, Contact? contact, JsonSerializ return jo.ToObject(); } - - public override bool CanWrite => true; - - public override bool CanRead => true; } \ No newline at end of file diff --git a/json-csharp/CustomJsonConverter/CustomJsonConverter/CustomJsonConverter.csproj b/json-csharp/CustomJsonConverter/CustomJsonConverter/CustomJsonConverter.csproj index 51eb283c58..ab6ddb3aff 100644 --- a/json-csharp/CustomJsonConverter/CustomJsonConverter/CustomJsonConverter.csproj +++ b/json-csharp/CustomJsonConverter/CustomJsonConverter/CustomJsonConverter.csproj @@ -1,13 +1,13 @@ - net7.0 + net10.0 enable enable - +