Struct LowLevelJsonParser

Source
pub struct LowLevelJsonParser { /* private fields */ }
Expand description

A low-level JSON parser acting on a provided buffer.

Does not allocate except a stack to check if array and object opening and closing are properly nested. This stack size might be limited using the method with_max_stack_size.

use json_event_parser::{JsonEvent, LowLevelJsonParser, LowLevelJsonParserResult};

let mut reader = LowLevelJsonParser::new();
assert!(matches!(
    reader.parse_next(b"{\"foo".as_slice(), false),
    LowLevelJsonParserResult {
        consumed_bytes: 1,
        event: Some(Ok(JsonEvent::StartObject))
    }
));
assert!(matches!(
    reader.parse_next(b"\"foo".as_slice(), false),
    LowLevelJsonParserResult {
        consumed_bytes: 0,
        event: None
    }
));
assert!(matches!(
    reader.parse_next(b"\"foo\": 1}".as_slice(), false),
    LowLevelJsonParserResult {
        consumed_bytes: 5,
        event: Some(Ok(JsonEvent::ObjectKey(Cow::Borrowed("foo"))))
    }
));
assert!(matches!(
    reader.parse_next(b": 1}".as_slice(), false),
    LowLevelJsonParserResult {
        consumed_bytes: 3,
        event: Some(Ok(JsonEvent::Number(Cow::Borrowed("1"))))
    }
));
assert!(matches!(
    reader.parse_next(b"}".as_slice(), false),
    LowLevelJsonParserResult {
        consumed_bytes: 1,
        event: Some(Ok(JsonEvent::EndObject))
    }
));
assert!(matches!(
    reader.parse_next(b"".as_slice(), true),
    LowLevelJsonParserResult {
        consumed_bytes: 0,
        event: Some(Ok(JsonEvent::Eof))
    }
));

Implementations§

Source§

impl LowLevelJsonParser

Source

pub const fn new() -> Self

Source

pub fn with_max_stack_size(self, size: usize) -> Self

Maximal allowed number of nested object and array openings. Infinite by default.

Source

pub fn parse_next<'a>( &mut self, input_buffer: &'a [u8], is_ending: bool, ) -> LowLevelJsonParserResult<'a>

Reads a new event from the data in input_buffer.

is_ending must be set to true if all the JSON data have been already consumed or are in input_buffer.

Source

pub fn read_next_event<'a>( &mut self, input_buffer: &'a [u8], is_ending: bool, ) -> LowLevelJsonParserResult<'a>

👎Deprecated: Use parse_next() instead

Trait Implementations§

Source§

impl Default for LowLevelJsonParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.