Skip to content

Repository files navigation

JSON streaming parser and serializer

actions status Latest Version Released API docs

JSON event parser is a simple streaming JSON parser and serializer implementation in Rust.

It does not aims to be the fastest JSON parser possible but to be a simple implementation allowing to parse larger than memory files.

If you want fast and battle-tested code you might prefer to use json, serde_json or simd-json.

Parser example:

use std::borrow::Cow;
use json_event_parser::{ReaderJsonParser, JsonEvent};

let json = b"{\"foo\": 1}";
let mut reader = ReaderJsonParser::new(json.as_slice());
assert!(matches!(reader.parse_next(), Some(Ok(JsonEvent::StartObject))));
assert!(matches!(reader.parse_next(), Some(Ok(JsonEvent::ObjectKey(Cow::Borrowed("foo"))))));
assert!(matches!(reader.parse_next(), Some(Ok(JsonEvent::Number(Cow::Borrowed("1"))))));
assert!(matches!(reader.parse_next(), Some(Ok(JsonEvent::EndObject))));
assert!(matches!(reader.parse_next(), None));
# std::io::Result::Ok(())

Serializer example:

use json_event_parser::{WriterJsonSerializer, JsonEvent};

let mut writer = WriterJsonSerializer::new(Vec::new());
writer.serialize_event(JsonEvent::StartObject)?;
writer.serialize_event(JsonEvent::ObjectKey("foo".into()))?;
writer.serialize_event(JsonEvent::Number("1".into()))?;
writer.serialize_event(JsonEvent::EndObject)?;

assert_eq!(writer.finish()?.as_slice(), b"{\"foo\":1}");
# std::io::Result::Ok(())

See json-event-parser-blocks for an implementation of serde on top of this crate (note that is project is not affiliated or endorsed by Oxigraph in any way).

License

This project is licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
  • MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in json-event-parser by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

JSON event parser is a simple streaming JSON parser and serializer implementation in Rust.

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages