Coder Social home page Coder Social logo

tstack / struson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcono1234/struson

0.0 1.0 0.0 329 KB

Streaming JSON reader and writer written in Rust

Home Page: https://crates.io/crates/struson

License: Apache License 2.0

Rust 100.00%

struson's Introduction

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.

Note: 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!

Why?

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

Usage examples

Reading

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

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

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

json_reader.end_object()?;
// Ensures that there is no trailing data
json_reader.consume_trailing_whitespace()?;

Writing

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);

json_writer.begin_object()?;
json_writer.name("a")?;

json_writer.begin_array()?;
json_writer.number_value(1)?;
json_writer.bool_value(true)?;
json_writer.end_array()?;

json_writer.end_object()?;
// Ensures that the JSON document is complete and flushes the buffer
json_writer.finish_document()?;

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.

Changelog

See GitHub releases.

Building

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

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.

struson's People

Contributors

marcono1234 avatar dependabot[bot] avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.