From b87928c9873889e6752c3bbb302e34dcbb59506d Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 30 Aug 2026 18:41:35 +0200 Subject: [PATCH] Property ordering in JSON: retarget net10.0, MSTest 4.3.3, fix converter Read() re-entry - All three projects net7.0 -> net10.0 - Newtonsoft.Json 13.0.3 -> 13.0.4, BenchmarkDotNet 0.13.5 -> 0.15.8, Microsoft.NET.Test.Sdk 17.3.2 -> 18.9.0, MSTest.TestAdapter/TestFramework 2.2.10 -> 4.3.3, coverlet.collector 3.1.2 -> 10.0.1 - MicrosoftOrderedPropertiesConverter.Read() called JsonSerializer.Deserialize with the same options the converter is registered in, so deserializing re-entered the converter and recursed until the process died with a stack overflow. It now throws NotSupportedException; the converter exists to control write order only. One test covers it. - Typo in a public member: Student.RegistratioNumber -> RegistrationNumber --- .../Benchmark/Benchmark.csproj | 4 ++-- .../Converters/MicrosoftOrderedPropertiesConverter.cs | 3 ++- .../OrderPropertyJsonInCSharp/Models/Student.cs | 2 +- .../OrderPropertyJsonInCSharp.csproj | 4 ++-- .../Tests/OrderPropertyJsonUnitTest.cs | 10 ++++++++++ .../OrderPropertyJsonInCSharp/Tests/Tests.csproj | 10 +++++----- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/json-csharp/OrderPropertyJsonInCSharp/Benchmark/Benchmark.csproj b/json-csharp/OrderPropertyJsonInCSharp/Benchmark/Benchmark.csproj index bde64bc598..0019c51ba0 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/Benchmark/Benchmark.csproj +++ b/json-csharp/OrderPropertyJsonInCSharp/Benchmark/Benchmark.csproj @@ -1,14 +1,14 @@ - net7.0 + net10.0 enable enable Exe - + diff --git a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Converters/MicrosoftOrderedPropertiesConverter.cs b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Converters/MicrosoftOrderedPropertiesConverter.cs index 664ab55095..fbc68a08a0 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Converters/MicrosoftOrderedPropertiesConverter.cs +++ b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Converters/MicrosoftOrderedPropertiesConverter.cs @@ -12,7 +12,8 @@ public override bool CanConvert(Type typeToConvert) public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - return JsonSerializer.Deserialize(ref reader, options); + throw new NotSupportedException( + "This converter only controls the order properties are written in. Deserialize without it."); } public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) diff --git a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Models/Student.cs b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Models/Student.cs index 9dbd5dfa0b..a65eaf4d1a 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Models/Student.cs +++ b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/Models/Student.cs @@ -2,7 +2,7 @@ { public class Student: Person { - public int RegistratioNumber { get; set; } + public int RegistrationNumber { get; set; } public double Grade { get; set; } } diff --git a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp.csproj b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp.csproj index 9cf5c969d2..6c67c2fd4a 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp.csproj +++ b/json-csharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp/OrderPropertyJsonInCSharp.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net10.0 enable enable - + diff --git a/json-csharp/OrderPropertyJsonInCSharp/Tests/OrderPropertyJsonUnitTest.cs b/json-csharp/OrderPropertyJsonInCSharp/Tests/OrderPropertyJsonUnitTest.cs index 45ad25e8e1..63850e7099 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/Tests/OrderPropertyJsonUnitTest.cs +++ b/json-csharp/OrderPropertyJsonInCSharp/Tests/OrderPropertyJsonUnitTest.cs @@ -69,5 +69,15 @@ public void WhenUsingMicrosoftTypeInfoResolver_ThenReturnOrderedProperties() Assert.AreEqual(expectedResult, json); } + + [TestMethod] + public void WhenDeserializingWithOrderedPropertiesConverter_ThenThrowsNotSupportedException() + { + var options = new System.Text.Json.JsonSerializerOptions(); + options.Converters.Add(new MicrosoftOrderedPropertiesConverter()); + + Assert.ThrowsExactly( + () => System.Text.Json.JsonSerializer.Deserialize("{\"Id\":1,\"Name\":\"Miau\",\"Age\":3}", options)); + } } } \ No newline at end of file diff --git a/json-csharp/OrderPropertyJsonInCSharp/Tests/Tests.csproj b/json-csharp/OrderPropertyJsonInCSharp/Tests/Tests.csproj index 38b26f5568..965189eade 100644 --- a/json-csharp/OrderPropertyJsonInCSharp/Tests/Tests.csproj +++ b/json-csharp/OrderPropertyJsonInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,10 +9,10 @@ - - - - + + + +