Serialize a list to JSON: retarget net10.0, add stream serialization - #2158
Merged
vladimir-pecanac-main merged 1 commit intoAug 31, 2026
Conversation
- Both projects net7.0 -> net10.0 (net7.0 is out of support). - Packages: BenchmarkDotNet 0.15.8, Newtonsoft.Json 13.0.4, Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1. - New SerializeToStreamAsync(Stream) on the System.Text.Json class, writing straight to the destination with JsonSerializer.SerializeAsync, plus a test asserting the stream contents. - Club.NumberOfPlayers is now an int. It held "26" as a string, so every JSON sample showed a quoted number in an article about JSON typing. Test expectations updated to match. - Primary constructors on both serializer classes; Program.cs to top-level statements. Build clean, 5/5 tests pass on SDK 10.0.302.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sample update for How to Serialize a List to JSON in C# (
json-csharp/HowToSerializeAListToJsonInCSharp).What changed
net7.0->net10.0. net7.0 is out of support.BenchmarkDotNet0.13.5 -> 0.15.8,Newtonsoft.Json13.0.2 -> 13.0.4,Microsoft.NET.Test.Sdk17.3.2 -> 18.9.0,xunit2.4.2 -> 2.9.3,xunit.runner.visualstudio2.4.5 -> 4.0.0,coverlet.collector3.1.2 -> 10.0.1.SerializeToStreamAsync(Stream)onSerializeListToJsonWithSystemTextJson, callingJsonSerializer.SerializeAsync(stream, clubList, _options), plus one test asserting the stream's contents match the expected JSON. The article gains a section on writing a list straight to a file or a stream, and this is the code behind it.Club.NumberOfPlayersis now anint. It was astringholding"26", so every JSON sample on the page printed"numberOfPlayers": "26"in quotes next to a bare"yearFounded": 1886- in an article about turning C# objects into JSON. Both test expectation strings updated with it.Program.cslifted to top-level statements.Verification
dotnet build -c Release: 0 warnings, 0 errors on SDK 10.0.302 (runtime 10.0.10).dotnet test -c Release: 5/5 passed (4 existing + the new stream test).[Benchmark]method names, so the article's console block is being replaced with the real output:The ordering the article states still holds on .NET 10: both
System.Text.Jsonmethods finish ahead of bothNewtonsoft.Jsonmethods, andSerialize()finishes ahead ofSerializeToUtf8Bytes().