Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'input, const MAX_DEPTH: usize = DEFAULT_MAX_DEPTH> { /* private fields */ }
Expand description

Streaming JSON parser over a borrowed byte slice.

Pull-based: each call to Parser::next_event advances the input and returns one Event. Returns Ok(None) once the document has been fully consumed (and any trailing whitespace has been validated).

Parser is the streaming API. It owns a Lexer (the byte walker) plus a small grammar state machine that enforces JSON’s “value, then comma, then value” structure when emitting events serially. Typed consumers (json_bourne::FromJson) drive the lexer directly and bypass this state machine — for them, the type’s recursive structure already enforces the grammar, and the dispatch through match self.state is pure overhead.

MAX_DEPTH is the maximum nesting depth of containers the parser will accept. It is also the size of the inline nesting stack, so picking a small value reduces the parser’s stack footprint as well as bounding untrusted input. The default is DEFAULT_MAX_DEPTH.

Implementations§

Source§

impl<'input, const MAX_DEPTH: usize> Parser<'input, MAX_DEPTH>

Source

pub const fn new(input: &'input [u8]) -> Self

Construct a parser over input.

§Panics

Panics if input.len() exceeds MAX_INPUT_LEN. See Lexer::new.

Source

pub const fn lexer(&mut self) -> &mut Lexer<'input, MAX_DEPTH>

Borrow the underlying lexer mutably. Typed consumers use this to drive parsing directly without going through the next_event state machine.

Source

pub const fn position(&self) -> Position

Source

pub const fn input(&self) -> &'input [u8]

The input slice the parser was constructed with.

Source

pub fn next_event(&mut self) -> Result<Option<Event>, Error>

Source

pub fn parse_i64_value(&mut self) -> Result<i64, Error>

Source

pub fn parse_str_value(&mut self) -> Result<&'input str, Error>

Source

pub fn array_start(&mut self) -> Result<bool, Error>

On [ push a frame and, for empty arrays, pop and synchronize state.

Source

pub fn array_continue(&mut self, end_byte: u8) -> Result<bool, Error>

Source

pub fn object_first_key(&mut self) -> Result<Option<&'input str>, Error>

Source

pub fn object_next_key(&mut self) -> Result<Option<&'input str>, Error>

Source

pub fn object_first_key_lex(&mut self) -> Result<Option<JsonStr>, Error>

Like object_first_key, but returns the key as a JsonStr span so the caller can decode escapes when present. The fast &str-returning variant rejects any backslash; this one carries escape-bearing keys through.

Source

pub fn object_next_key_lex(&mut self) -> Result<Option<JsonStr>, Error>

Like object_next_key, but returns the key as a JsonStr span. See object_first_key_lex.

Trait Implementations§

Source§

impl<'input, const MAX_DEPTH: usize> Debug for Parser<'input, MAX_DEPTH>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'input, const MAX_DEPTH: usize> Freeze for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> RefUnwindSafe for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> Send for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> Sync for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> Unpin for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> UnsafeUnpin for Parser<'input, MAX_DEPTH>

§

impl<'input, const MAX_DEPTH: usize> UnwindSafe for Parser<'input, MAX_DEPTH>

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.