Can Json.NET serialize / deserialize to / from a stream?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes. Json.NET can serialize directly to a Stream and deserialize directly from one. In practice you do this by wrapping the stream with StreamReader or StreamWriter, then connecting Json.NET through JsonTextReader or JsonTextWriter.
Writing JSON Directly to a Stream
The core API is JsonSerializer.Serialize. It does not write bytes by itself. It writes JSON text through a text writer, which is why you normally combine it with StreamWriter.
Two details matter here. First, the stream stays open because leaveOpen: true is set on StreamWriter. Second, the stream position is reset before reading back the bytes.
Reading JSON From a Stream
Deserialization is the mirror image of serialization. You read bytes from a stream, decode them to text, and let JsonTextReader feed tokens into JsonSerializer.Deserialize.
This is the normal pattern for files, HTTP request bodies, queue payloads, and any other JSON source that naturally arrives as a stream.
Why Streams Are Better Than Temporary Strings
If the JSON already exists in a file, network socket, or request body, reading the whole payload into a giant string first adds extra memory pressure and an unnecessary copy. Stream-based processing is usually the cleaner design because:
- the transport already gives you a stream
- large payloads do not need a second full in-memory representation
- the API composes naturally with file and network code
That does not mean Json.NET becomes fully incremental by magic. If you deserialize a huge array into a List<T>, the objects still have to exist in memory. The stream only removes the extra string layer.
Streaming Large Arrays Safely
For large JSON arrays, you can read one object at a time instead of materializing everything up front. This is useful in ETL jobs and import pipelines.
This pattern is not appropriate for every schema, but it is much friendlier to memory than loading a very large JArray when all you really need is sequential processing.
Files and HTTP Bodies
The same pattern works with real streams from the filesystem or the network.
On the read side, an ASP.NET Core controller or middleware can deserialize directly from HttpRequest.Body using the same reader stack.
Common Pitfalls
One common mistake is disposing the text writer and then trying to reuse the stream without leaveOpen: true. Another is forgetting to flush buffered writers, which can make the output look truncated.
Encoding mismatches are also easy to miss. JSON is usually UTF-8, so make that explicit. Finally, if you write to a MemoryStream and then read it back, reset Position to 0 first.
Summary
- Json.NET can serialize to and deserialize from streams without first creating a full JSON string.
- Use
StreamWriterplusJsonTextWriterfor output andStreamReaderplusJsonTextReaderfor input. - Stream-based handling reduces extra memory copies and fits file and network APIs naturally.
- Large arrays can be processed incrementally by reading one object at a time.
- Watch disposal, flushing, encoding, and stream position carefully.

