Expand description
Streaming parser for JSON. The main type is JsonSession
, which consumes an iterator over
bytes and yields JsonFragmentWithSpan
values. JsonFragment
has the following enum variants:
BeginObject
, ObjectProperty
, EndObject
, BeginArray
, EndArray
, and PrimitiveValue
.
Every yielded value has a LocationSpan
attached to it, saying at which byte offset
(and at which line and column) the fragment begins and ends.
This API allows gathering statistics about the contents of large JSON documents without ever holding the entire document in memory.
use json_session::{JsonSession, JsonFragment, JsonPrimitiveValue};
let input_str = r#"{"key1": 1234, "key2": [true], "key3": "value" }"#;
let expected = &[
JsonFragment::BeginObject,
JsonFragment::ObjectProperty(String::from("key1")),
JsonFragment::PrimitiveValue(JsonPrimitiveValue::Number(1234.0)),
JsonFragment::ObjectProperty(String::from("key2")),
JsonFragment::BeginArray,
JsonFragment::PrimitiveValue(JsonPrimitiveValue::Boolean(true)),
JsonFragment::EndArray,
JsonFragment::ObjectProperty(String::from("key3")),
JsonFragment::PrimitiveValue(JsonPrimitiveValue::String(String::from("value"))),
JsonFragment::EndObject,
];
let mut session = JsonSession::new(input_str.as_bytes().iter().cloned());
for expected_fragment in expected {
let fragment = session.next().unwrap().unwrap().fragment;
assert_eq!(fragment, *expected_fragment);
}
assert!(session.next().unwrap().is_none());
JsonSession
checks that the JSON is valid, and only returns a sequence of fragments
that describe valid JSON. If an invalid sequence of bytes or tokens is detected,
JsonSession::next
yields an error.
This crate also exposes the tokenizer and token types that are used internally, but you can ignore them.
Structs§
- Json
Fragment With Span - A
JsonFragment
paired with aLocationSpan
. - Json
Parse Error - The error type used in this crate. Comes with Location information.
- Json
Session - A pull-based JSON parser which consumes an iterator over bytes and yields
a valid sequence of
JsonFragmentWithSpan
values. - Json
Tokenizer - A pull-based tokenizer which takes an iterator over bytes and emits
JsonToken
s. - Location
- A byte offset and the corresponding line and column number.
- Location
Span - The start and end
Location
of a fragment.
Enums§
- Json
Fragment - A fragment of JSON. This is a bit more high-level than a token.
- Json
Primitive Value - A JSON value which is not an object or an array.
- Json
Token - A JSON token.
Type Aliases§
- Json
Parse Result - A type alias for
Result<T, JsonParseError>
.