Skip to content

Serialize a list to JSON: retarget net10.0, add stream serialization - #2158

Merged
vladimir-pecanac-main merged 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/84615-list-to-json
Aug 31, 2026
Merged

Serialize a list to JSON: retarget net10.0, add stream serialization#2158
vladimir-pecanac-main merged 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/84615-list-to-json

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for How to Serialize a List to JSON in C# (json-csharp/HowToSerializeAListToJsonInCSharp).

What changed

  • Both projects retargeted net7.0 -> net10.0. net7.0 is out of support.
  • Packages bumped (versions read from NuGet on the day, not from memory): BenchmarkDotNet 0.13.5 -> 0.15.8, Newtonsoft.Json 13.0.2 -> 13.0.4, Microsoft.NET.Test.Sdk 17.3.2 -> 18.9.0, xunit 2.4.2 -> 2.9.3, xunit.runner.visualstudio 2.4.5 -> 4.0.0, coverlet.collector 3.1.2 -> 10.0.1.
  • New SerializeToStreamAsync(Stream) on SerializeListToJsonWithSystemTextJson, calling JsonSerializer.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.NumberOfPlayers is now an int. It was a string holding "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.
  • Primary constructors on the two serializer classes, and Program.cs lifted 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 re-run on net10.0. The published table's row labels never matched the [Benchmark] method names, so the article's console block is being replaced with the real output:
| Method                                   | Mean     | Error     | StdDev    | Allocated |
|----------------------------------------- |---------:|----------:|----------:|----------:|
| SystemTextJsonSerializeMethod            | 3.297 ms | 0.0560 ms | 0.0496 ms |   2.23 MB |
| SystemTextJsonSerializeToUtf8BytesMethod | 3.404 ms | 0.0668 ms | 0.0769 ms |   3.35 MB |
| NewtonsoftJsonJsonSerializerClass        | 4.739 ms | 0.0716 ms | 0.0635 ms |   4.95 MB |
| NewtonsoftJsonSerializeObjectMethod      | 4.800 ms | 0.0545 ms | 0.0509 ms |   4.94 MB |

The ordering the article states still holds on .NET 10: both System.Text.Json methods finish ahead of both Newtonsoft.Json methods, and Serialize() finishes ahead of SerializeToUtf8Bytes().

- 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.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 7efa5b7 into CodeMazeBlog:main Aug 31, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant