Struct SliceParser

Source
pub struct SliceParser<'a, 'b, C: BitStackConfig = DefaultConfig> { /* private fields */ }
Expand description

A pull parser that parses JSON from a slice.

Generic over BitStack storage type for configurable nesting depth.

Implementations§

Source§

impl<'a> SliceParser<'a, '_, DefaultConfig>

Methods for the pull parser.

Source

pub fn new(input: &'a str) -> Self

Creates a new parser for the given JSON input.

This parser assumes no string escapes will be encountered. If escapes are found, parsing will fail with ScratchBufferFull error.

For JSON with potential string escapes, use with_buffer() instead.

§Arguments
  • input - A string slice containing the JSON data to be parsed.
§Example
use picojson::SliceParser;
let parser = SliceParser::new(r#"{"name": "value"}"#);
Source

pub fn new_from_slice(input: &'a [u8]) -> Self

Creates a new parser from a byte slice.

Assumes no string escapes will be encountered. For JSON with escapes, use with_buffer_from_slice.

§Example
let parser = SliceParser::new_from_slice(br#"{"name": "value"}"#);
Source§

impl<'a, 'b> SliceParser<'a, 'b, DefaultConfig>

Constructor with scratch buffer for SliceParser using DefaultConfig

Source

pub fn with_buffer(input: &'a str, scratch_buffer: &'b mut [u8]) -> Self

Creates a new parser for the given JSON input with external scratch buffer.

Use this when your JSON contains string escapes (like \n, \", \u0041) that need to be unescaped during parsing.

§Arguments
  • input - A string slice containing the JSON data to be parsed.
  • scratch_buffer - A mutable byte slice for temporary string unescaping operations. This buffer needs to be at least as long as the longest contiguous token (string, key, number) in the input.
§Example
use picojson::SliceParser;
let mut scratch = [0u8; 1024];
let parser = SliceParser::with_buffer(r#"{"msg": "Hello\nWorld"}"#, &mut scratch);
Source

pub fn with_buffer_from_slice( input: &'a [u8], scratch_buffer: &'b mut [u8], ) -> Self

Creates a new parser from a byte slice with a scratch buffer.

Use when JSON contains string escapes that need unescaping.

§Example
let mut scratch = [0u8; 1024];
let parser = SliceParser::with_buffer_from_slice(br#"{"msg": "Hello\nWorld"}"#, &mut scratch);
Source§

impl<'a, 'b, C: BitStackConfig> SliceParser<'a, 'b, C>

Generic constructor for SliceParser with custom configurations

Source

pub fn with_config(input: &'a str) -> Self

Creates a new parser with a custom BitStackConfig.

This parser assumes no string escapes will be encountered. If escapes are found, parsing will fail. For JSON with escapes, use with_config_and_buffer.

Source

pub fn with_config_from_slice(input: &'a [u8]) -> Self

Creates a new parser from a byte slice with a custom BitStackConfig.

Assumes no string escapes will be encountered. For JSON with escapes, use with_config_and_buffer_from_slice.

Source

pub fn with_config_and_buffer( input: &'a str, scratch_buffer: &'b mut [u8], ) -> Self

Creates a new parser with a custom BitStackConfig and a user-provided scratch buffer.

Use this when your JSON contains string escapes (like \n, \", \u0041).

§Arguments
  • input - A string slice containing the JSON data to be parsed.
  • scratch_buffer - A mutable byte slice for temporary string unescaping operations. This buffer needs to be at least as long as the longest contiguous token (string, key, number) in the input.
Source

pub fn with_config_and_buffer_from_slice( input: &'a [u8], scratch_buffer: &'b mut [u8], ) -> Self

Creates a new parser from a byte slice with a custom BitStackConfig and scratch buffer.

Use when JSON contains string escapes that need unescaping. This is the core constructor that all other constructors delegate to.

Trait Implementations§

Source§

impl<'a, 'b, C: BitStackConfig> PullParser for SliceParser<'a, 'b, C>

Source§

fn next_event(&mut self) -> Result<Event<'_, '_>, ParseError>

Returns the next JSON event or an error if parsing fails. Parsing continues until EndDocument is returned or an error occurs.
Source§

fn next(&mut self) -> Option<Result<Event<'_, '_>, ParseError>>

Iterator-like method that returns None when parsing is complete. This method returns None when EndDocument is reached, Some(Ok(event)) for successful events, and Some(Err(error)) for parsing errors.

Auto Trait Implementations§

§

impl<'a, 'b, C> Freeze for SliceParser<'a, 'b, C>

§

impl<'a, 'b, C> RefUnwindSafe for SliceParser<'a, 'b, C>

§

impl<'a, 'b, C> Send for SliceParser<'a, 'b, C>

§

impl<'a, 'b, C> Sync for SliceParser<'a, 'b, C>

§

impl<'a, 'b, C> Unpin for SliceParser<'a, 'b, C>

§

impl<'a, 'b, C = DefaultConfig> !UnwindSafe for SliceParser<'a, 'b, C>

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.