TL;DR: JsonSerializer.SerializeAsyncEnumerable(stream, items, topLevelValues: true) writes canonical JSON Lines: one compact value per line, LF after every one. No more WriteLine(JsonSerializer.Serialize(x)) loops.
This ships in .NET 11, which is at Release Candidate 2 with a go-live license, but is not GA yet. Everything below was run on the .NET 11 RC 1 SDK.
The loop everybody has written
JSON Lines (JSONL) is one JSON value per line. It is the format of choice for log files, data exports, and anything you want to append to without rewriting the whole file.
System.Text.Json could already read it. On .NET 10, DeserializeAsyncEnumerable(stream, topLevelValues: true) streams top-level values just fine. Writing it was on you:
await using var writer = new StreamWriter ( "orders.jsonl" ); await foreach ( var order in LoadOrders ()) { await writer . WriteLineAsync ( JsonSerializer .…