You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JSON Lines (JSONL) is a text-based data format where each line represents a valid JSON object. This structure allows for easy parsing and processing of data on a line-by-line basis, making it suitable for handling large datasets. efficiently, and streaming content. JSONL is used by A2UI (Agent to UI), a declarative UI protocol for agent-driven interfaces.
Consider adding framework support for producing and consuming JSONL documents including a serialization and deserialization model. This support could then be used to build A2UI support for .NET.
.NET 9 shipped comprehensive JSONL deserialization support (#33030). At the low level, JsonReaderOptions.AllowMultipleValues permits the Utf8JsonReader to consume multiple top-level JSON values separated by whitespace. At the high level, JsonSerializer.DeserializeAsyncEnumerable<T> with topLevelValues: true streams JSONL documents from a Stream or PipeReader:
Without topLevelValues (or when set to false), the same method reads from a root-level JSON array instead. Note that the reader is lenient: it accepts any whitespace (spaces, tabs, \r\n, \n) between documents, which is a superset of strict JSONL. This is fine — Postel's law applies.
Serialization
There is no dedicated JSONL serialization API today, but the same result can be achieved by calling SerializeAsync in a loop and writing newline separators manually:
Both approaches work but require boilerplate that is easy to get wrong (forgetting the newline, forgetting to reset, not flushing). The proposed API below encapsulates this pattern and mirrors the symmetry of the existing DeserializeAsyncEnumerable API.
When topLevelValues is false (the default), the output is a root-level JSON array [v1,v2,...]. When topLevelValues is true, the output is JSON Lines (one value per line). This mirrors the existing DeserializeAsyncEnumerable API exactly.
The newline separator emitted in JSONL mode respects JsonSerializerOptions.NewLine for consistency with Utf8JsonWriter indentation behavior. The JSONL spec accepts both \n and \r\n.
Utf8JsonWriter.AllowMultipleValues with automatic newline insertion: Considered and prototyped, but rejected. Adding state tracking to the writer's hot path for an uncommon scenario is not justified when Reset() already provides a clean mechanism.
Risks
No changes to Utf8JsonWriter internals — zero hot-path impact.
The JSONL implementation uses writer.Reset() between values, which is an existing public API with well-defined semantics.
Do I understand it correctly that this makes it easy to produce JSONL, but it does not enforce the JSONL spec? So if I set JsonSerializerOptions.WriteIndented to true or if I serialize arrays or primitive types (JSONL mandates only objects at the top level), then I will get invalid JSONL?
Do I understand it correctly that this makes it easy to produce JSONL, but it does not enforce the JSONL spec?
Only on the deserialization side, following Postel's law.
So if I set JsonSerializerOptions.WriteIndented to true or if I serialize arrays or primitive types (JSONL mandates only objects at the top level), then I will get invalid JSONL?
Indentation settings should be ignored when used by these methods specifically. When it comes to primitive types or arrays, yes, individual top level values may not get serialized as objects, but the same holds for regular serialization which may or may not produce valid JSON documents for the same reason.
Activity
dotnet-policy-service commentedon Apr 1, 2026
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Apr 1, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
eiriktsarpalis commentedon Apr 1, 2026
.NET 9 shipped comprehensive JSONL deserialization support (#33030). At the low level,
JsonReaderOptions.AllowMultipleValuespermits theUtf8JsonReaderto consume multiple top-level JSON values separated by whitespace. At the high level,JsonSerializer.DeserializeAsyncEnumerable<T>withtopLevelValues: truestreams JSONL documents from aStreamorPipeReader:Without
topLevelValues(or when set tofalse), the same method reads from a root-level JSON array instead. Note that the reader is lenient: it accepts any whitespace (spaces, tabs,\r\n,\n) between documents, which is a superset of strict JSONL. This is fine — Postel's law applies.Serialization
There is no dedicated JSONL serialization API today, but the same result can be achieved by calling
SerializeAsyncin a loop and writing newline separators manually:Or, for better performance, using
Utf8JsonWriterwithReset()to reuse writer state across values:Both approaches work but require boilerplate that is easy to get wrong (forgetting the newline, forgetting to reset, not flushing). The proposed API below encapsulates this pattern and mirrors the symmetry of the existing
DeserializeAsyncEnumerableAPI.API Proposal
When
topLevelValuesisfalse(the default), the output is a root-level JSON array[v1,v2,...]. WhentopLevelValuesistrue, the output is JSON Lines (one value per line). This mirrors the existingDeserializeAsyncEnumerableAPI exactly.The newline separator emitted in JSONL mode respects
JsonSerializerOptions.NewLinefor consistency withUtf8JsonWriterindentation behavior. The JSONL spec accepts both\nand\r\n.API Usage
Alternative Designs
Utf8JsonWriter.AllowMultipleValueswith automatic newline insertion: Considered and prototyped, but rejected. Adding state tracking to the writer's hot path for an uncommon scenario is not justified whenReset()already provides a clean mechanism.Risks
Utf8JsonWriterinternals — zero hot-path impact.writer.Reset()between values, which is an existing public API with well-defined semantics.Prototype: af783d6c224
svick commentedon Apr 28, 2026
Do I understand it correctly that this makes it easy to produce JSONL, but it does not enforce the JSONL spec? So if I set
JsonSerializerOptions.WriteIndentedtotrueor if I serialize arrays or primitive types (JSONL mandates only objects at the top level), then I will get invalid JSONL?eiriktsarpalis commentedon Apr 28, 2026
Only on the deserialization side, following Postel's law.
Indentation settings should be ignored when used by these methods specifically. When it comes to primitive types or arrays, yes, individual top level values may not get serialized as objects, but the same holds for regular serialization which may or may not produce valid JSON documents for the same reason.
bartonjs commentedon Apr 28, 2026
Video
Looks good as proposed
Add JSONL serialization support to System.Text.Json (#127567)
Add JSONL serialization support to System.Text.Json (#127567)