Struson
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.

:information_source: This library is still experimental. The performance is not very good yet and the API might be changed in future versions. Feedback and suggestions for improvements are welcome!

Why?

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

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

Usage examples

Reading

```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!(jsonreader.next_name()?, "a");

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()?; ```

Writing

```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::::new(); let mut json_writer = JsonStreamWriter::new(&mut writer);

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]}"#, std::str::fromutf8(&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.

Changelog

See GitHub releases.

Building

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.

Similar projects

License

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.