Menu

Stop Hand-Rolling JSON Lines With WriteLine. .NET 11 Writes Them for You
📰

Stop Hand-Rolling JSON Lines With WriteLine. .NET 11 Writes Them for You

DEV Community: dotnet·JKC·8 days ago
#m3jXbgcF
#devtodotnet#devto#dev#json#lines
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.