#json #streaming #parser


A low-level streaming JSON reader and writer

3 releases (breaking)

0.3.0 Sep 5, 2023
0.2.0 Aug 6, 2023
0.1.0 Apr 22, 2023

#232 in Parser implementations

Download history 3/week @ 2023-08-16 149/week @ 2023-08-23 218/week @ 2023-08-30 304/week @ 2023-09-06 262/week @ 2023-09-13 376/week @ 2023-09-20 253/week @ 2023-09-27 411/week @ 2023-10-04 314/week @ 2023-10-11 361/week @ 2023-10-18 728/week @ 2023-10-25 715/week @ 2023-11-01 624/week @ 2023-11-08 693/week @ 2023-11-15 359/week @ 2023-11-22 391/week @ 2023-11-29

2,219 downloads per month
Used in sindit-senml


10K SLoC

crates.io docs.rs

Struson is an RFC 8259 compliant streaming JSON reader and writer.

Its main purpose is to allow writing JSON documents in a memory efficient way without having to store the complete JSON document structure in memory.

The API of Struson was inspired by the streaming API of the Java library Gson (classes JsonReader and JsonWriter). It is rather low-level and its methods correspond to the elements of a JSON document, with little abstraction on top of it, allowing to read and write any valid JSON document regardless of its structure or content.

ℹ This library is still experimental. The performance is not very good yet and the API might be changed in future versions; releases < 1.0.0 might not follow Semantic Versioning, breaking changes may occur.
Feedback and suggestions for improvements are welcome!


The most popular JSON Rust crates Serde JSON (serde_json) and json-rust (json) mainly provide high level APIs for working with JSON.

  • Serde JSON provides an API for converting JSON into DOM like structures (module serde_json::value) and object mapper functionality by converting structs to JSON and vice versa. Both requires the complete value to be present in memory. The trait serde_json::ser::Formatter actually allows writing JSON in a streaming way, but its API is arguably too low level and inconvenient to use: You have to handle string escaping yourself, and you always have to provide the writer as argument for every method call.
    Note however, that Serde JSON's StreamDeserializer allows reading multiple top-level values in a streaming way, and that certain streaming use cases can be solved with custom Visitor implementations, see the documentation for examples of streaming an array and discarding data.

  • json-rust provides an API for converting JSON into DOM like structures (enum json::JsonValue), this requires the complete value to be present in memory. The trait json::codegen::Generator offers a partial API for writing JSON in a streaming way, however it misses methods for writing JSON arrays and objects in a streaming way.

If you need to process JSON in a DOM like way or want object mapper functionality to convert structs to JSON and vice versa, then Struson is not suited for your use case and you should instead use one of the libraries above.

Main features

  • Low level streaming API, no implicit value conversion
  • Strong enforcement of correct API usage
  • Panics only for incorrect API usage
    Malformed JSON and unexpected JSON structure only causes errors
  • API does not require recursion for JSON arrays and objects
    Can theoretically read and write arbitrarily deeply nested JSON data
  • Read arbitrarily precise JSON numbers as string
  • Seek to specific location in JSON data (JsonReader::seek_to)
  • Transfer JSON data from a reader to a writer (JsonReader::transfer_to)
  • Read and write arbitrarily large JSON string values
    (JsonReader::next_string_reader and JsonWriter::string_value_writer)
  • Optional Serde integration

Usage examples


use struson::reader::*;
// In this example JSON data comes from a string;
// normally it would come from a file or a network connection
let json = r#"{"a": [1, true]}"#;
let mut json_reader = JsonStreamReader::new(json.as_bytes());

assert_eq!("a", json_reader.next_name()?);

assert_eq!(1_u32, json_reader.next_number()??);
assert_eq!(true, json_reader.next_bool()?);

// Ensures that there is no trailing data


use struson::writer::*;
// In this example JSON bytes are stored in a Vec;
// normally they would be written to a file or network connection
let mut writer = Vec::<u8>::new();
let mut json_writer = JsonStreamWriter::new(&mut writer);



// Ensures that the JSON document is complete and flushes the buffer

assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?);

Serde integration

Optional integration with Serde exists to allow writing a Serialize to a JsonWriter and reading a Deserialize from a JsonReader. See the serde module of this crate for more information.


See GitHub releases.


This project uses cargo-make for building:

cargo make

If you don't want to install cargo-make, you can instead manually run the tasks declared in the Makefile.toml.

Similar projects


Licensed under either of

at your option.

All contributions you make to this project are licensed implicitly under both licenses mentioned above, without any additional terms or conditions.

Note: This dual-licensing is the same you see for the majority of Rust projects, see also the Rust API Guidelines.


~21K SLoC