Struct JsonSession

Source
pub struct JsonSession<I: Iterator<Item = u8>> { /* private fields */ }
Expand description

A pull-based JSON parser which consumes an iterator over bytes and yields a valid sequence of JsonFragmentWithSpan values.

This API allows gathering statistics about the contents of large JSON documents without ever holding the entire document in memory.

JsonSession checks that the input is valid JSON. If an invalid sequence of tokens is detected, JsonSession::next yields an error. As a user of JsonSession, you can rely on the fact that the yielded fragments will always describe a well-formed JSON document, at least the part of the document that has been consumed so far. (To clarify, there is no pre-pass which validates the entire document. Validation happens as you go, so JsonSession::next will happily return fragments as long as it hasn’t arrived at the error yet.)

When the optional feature fallible-iterator is used, JsonSession implements fallible_iterator::FallibleIterator.

§Example

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

Implementations§

Source§

impl<I: Iterator<Item = u8>> JsonSession<I>

Source

pub fn new(it: I) -> Self

Create a new JsonSession from an iterator over bytes.

Source

pub fn next(&mut self) -> JsonParseResult<Option<JsonFragmentWithSpan>>

Get the next JsonFragmentWithSpan.

Returns:

  • Ok(Some(...)) in the regular case, with the next fragment.
  • Ok(None) if the JSON document is complete and the end of the input has been reached.
  • Err(...) if a invalid JSON is detected.

Auto Trait Implementations§

§

impl<I> Freeze for JsonSession<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for JsonSession<I>
where I: RefUnwindSafe,

§

impl<I> Send for JsonSession<I>
where I: Send,

§

impl<I> Sync for JsonSession<I>
where I: Sync,

§

impl<I> Unpin for JsonSession<I>
where I: Unpin,

§

impl<I> UnwindSafe for JsonSession<I>
where I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.