Skip to main content

IncrementalNdjsonParser

Struct IncrementalNdjsonParser 

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

Incremental NDJSON parser that yields complete JSON objects as they arrive.

§How It Works

The parser maintains a buffer of received bytes and tracks brace nesting depth. When the depth returns to zero after seeing a closing brace, we have a complete JSON object that can be parsed.

§Depth Limit

The parser enforces a maximum nesting depth to prevent integer overflow from malicious input with extremely deep nesting. If the depth exceeds this limit, parsing fails with an error.

§Example

let mut parser = IncrementalNdjsonParser::new();

// Feed first half of JSON
let (parser, events1) = parser.feed_and_get_events(b"{\"type\": \"de");
assert_eq!(events1.len(), 0);  // Not complete yet

// Feed second half
let (_, events2) = parser.feed_and_get_events(b"lta\"}\n");
assert_eq!(events2.len(), 1);  // Complete!
assert_eq!(events2[0], "{\"type\": \"delta\"}");

Implementations§

Source§

impl IncrementalNdjsonParser

Source

pub const fn new() -> Self

Create a new incremental NDJSON parser.

Source

pub fn feed(self, byte: u8) -> Self

Feed bytes into the parser, returning any complete JSON objects found.

This method processes the input bytes and extracts complete JSON objects. Multiple JSON objects may be returned from a single call if they’re all complete.

§Arguments
  • data - Bytes to feed into the parser
§Returns

A tuple of (updated parser, vector of complete JSON strings), in the order they were completed.

Source

pub fn feed_and_get_events(self, data: &[u8]) -> (Self, Vec<String>)

Source

pub fn drain_results(&mut self) -> Vec<String>

Source

pub fn get_results(&self) -> Vec<String>

Get any complete JSON objects extracted so far.

Source

pub const fn is_parsing(&self) -> bool

Check if the parser is currently inside a JSON object.

Source

pub fn finish(self) -> Option<String>

Finalize parsing and return any remaining buffered data.

This method should be called when the input stream ends to retrieve any incomplete JSON that was buffered. This is important for handling cases where the last line of a file doesn’t have a trailing newline or where a complete JSON object was received but not yet extracted.

§Returns

Any remaining buffered data as a string if non-empty, or None if buffer is empty.

§Example
let mut parser = IncrementalNdjsonParser::new();
let (_, _) = parser.feed_and_get_events(b"{\"type\": \"delta\"}\n{\"type\": \"incomplete\"");
// When stream ends, get any remaining buffered data
if let Some(remaining) = parser.finish() {
    println!("Remaining: {}", remaining);
}

Trait Implementations§

Source§

impl Default for IncrementalNdjsonParser

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.