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.
JsonReader::seek_to
)JsonReader::transfer_to
)JsonReader::next_string_reader
and JsonWriter::string_value_writer
)```rust 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 jsonreader = JsonStreamReader::new(json.asbytes());
jsonreader.beginobject()?; asserteq!("a", jsonreader.next_name()?);
jsonreader.beginarray()?; asserteq!(1u32, jsonreader.nextnumber()??); asserteq!(true, jsonreader.nextbool()?); jsonreader.end_array()?;
jsonreader.endobject()?; // Ensures that there is no trailing data jsonreader.consumetrailing_whitespace()?; ```
```rust
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::
jsonwriter.beginobject()?; json_writer.name("a")?;
jsonwriter.beginarray()?; jsonwriter.numbervalue(1)?; jsonwriter.boolvalue(true)?; jsonwriter.endarray()?;
jsonwriter.endobject()?; // Ensures that the JSON document is complete and flushes the buffer jsonwriter.finishdocument()?;
asserteq!(r#"{"a":[1,true]}"#, String::fromutf8(writer)?); ```
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:
sh
cargo make
If you don't want to install cargo-make, you can instead manually run the tasks declared in the Makefile.toml
.
justjson::parser::Tokenizer
> A JSON tokenizer, which converts JSON source to a series of TokensParser
(deprecated)
> A streaming JSON parser implemented as an iterator of JsonEvent, consuming an iterator of char.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.