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>
impl<'input, const MAX_DEPTH: usize> Parser<'input, MAX_DEPTH>
Sourcepub const fn lexer(&mut self) -> &mut Lexer<'input, MAX_DEPTH>
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.
pub const fn position(&self) -> Position
pub fn next_event(&mut self) -> Result<Option<Event>, Error>
pub fn parse_i64_value(&mut self) -> Result<i64, Error>
pub fn parse_str_value(&mut self) -> Result<&'input str, Error>
Sourcepub fn array_start(&mut self) -> Result<bool, Error>
pub fn array_start(&mut self) -> Result<bool, Error>
On [ push a frame and, for empty arrays, pop and synchronize state.
pub fn array_continue(&mut self, end_byte: u8) -> Result<bool, Error>
pub fn object_first_key(&mut self) -> Result<Option<&'input str>, Error>
pub fn object_next_key(&mut self) -> Result<Option<&'input str>, Error>
Sourcepub fn object_first_key_lex(&mut self) -> Result<Option<JsonStr>, Error>
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.
Sourcepub fn object_next_key_lex(&mut self) -> Result<Option<JsonStr>, Error>
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.