{
  "id": 18412838,
  "title": "Stop Hand-Rolling JSON Lines With WriteLine. .NET 11 Writes Them for You",
  "url": "https://urgent.news/2026/11/06/stop-hand-rolling-json-lines-with-writeline-net-11-writes-them-for-you",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-06T01:11:53.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/iamprincejkc/stop-hand-rolling-json-lines-with-writeline-net-11-writes-them-for-you-13aj"
  },
  "original_language": "en",
  "account": "JSON Lines (JSONL) is a popular format for log files, data exports, and appending data without rewriting the entire file. Previously, System.Text.Json in .NET required hand-rolled loops to write JSONL files. However, with .NET 11, this process has become much simpler.\n\n.NET 11 introduces four new overloads for JsonSerializer.SerializeAsyncEnumerable: one that writes to a Stream or PipeWriter and another with a bool parameter called topLevelValues. When topLevelValues is set to false (the default), the output will be one JSON array, similar to the existing SerializeAsync method. When topLevelValues is set to true, it produces JSON Lines, where each value is compactly written with a newline character after it, including the last one.\n\nThis new feature uses LF as the line separator, regardless of JsonSerializerOptions.NewLine settings, and it ignores WriteIndented. The minimal example demonstrates how to write JSONL data using these new methods:\n\n```csharp\nusing System.Text.Json;\nvar options = new JsonSerializerOptions(JsonSerializerDefaults.Web);\nawait using var file = File.Create(\"orders.jsonl\");\nawait JsonSerializer.SerializeAsyncEnumerable(file, LoadOrders(), topLevelValues: true, options: options);\n```\n\nThe provided LoadOrders function generates two sample Order records, which are then written to a file in JSONL format. When compared to the hand-rolled loop using WriteIndented = true, the new JSONL mode results in a much smaller output file (78 bytes) with only two lines and no carriage returns.\n\nReading JSONL files back in is straightforward using the existing DeserializeAsyncEnumerable method:\n\n```csharp\nawait foreach (var order in JsonSerializer.DeserializeAsyncEnumerable<Order>(stream, topLevelValues: true, options: options))\n{\nConsole.WriteLine(order);\n}\n```\n\nBoth orders are successfully deserialized and displayed intact. The PipeWriter overload of the new SerializeAsyncEnumerable methods is particularly useful for ASP.NET Core applications, where Response.BodyWriter is already a PipeWriter. This allows developers to create a streaming JSONL endpoint:\n\n```csharp\napp.MapGet(\"/orders.jsonl\", async (HttpContext ctx, CancellationToken ct) =>\n{\nctx.Response.ContentType = \"application/jsonl\";\nawait JsonSerializer.SerializeAsyncEnumerable(ctx.Response.BodyWriter, LoadOrders(ct), AppJson.Default.Order, topLevelValues: true, cancellationToken: ct);\n});\n```\n\nThis example demonstrates a JSONL endpoint that streams orders as they come from a slow source, making it easy to integrate with a REST API.",
  "summary": "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…",
  "key_points": [
    ".NET 11 introduces JSONL writing with JsonSerializer.SerializeAsyncEnumerable",
    "topLevelValues parameter creates JSON Lines format with newline separation",
    "PipeWriter overload enables streaming JSONL endpoints in ASP.NET Core"
  ],
  "editors_take": "Automating JSON Lines serialization in .NET 11 removes the need for manual loops, allowing developers to build more efficient streaming endpoints for data exports and REST APIs.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}