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
impl IncrementalNdjsonParser
Sourcepub fn feed(self, byte: u8) -> Self
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.
pub fn feed_and_get_events(self, data: &[u8]) -> (Self, Vec<String>)
pub fn drain_results(&mut self) -> Vec<String>
Sourcepub fn get_results(&self) -> Vec<String>
pub fn get_results(&self) -> Vec<String>
Get any complete JSON objects extracted so far.
Sourcepub const fn is_parsing(&self) -> bool
pub const fn is_parsing(&self) -> bool
Check if the parser is currently inside a JSON object.
Sourcepub fn finish(self) -> Option<String>
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§
Auto Trait Implementations§
impl Freeze for IncrementalNdjsonParser
impl RefUnwindSafe for IncrementalNdjsonParser
impl Send for IncrementalNdjsonParser
impl Sync for IncrementalNdjsonParser
impl Unpin for IncrementalNdjsonParser
impl UnsafeUnpin for IncrementalNdjsonParser
impl UnwindSafe for IncrementalNdjsonParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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