pub struct StreamingDecoder { /* private fields */ }Expand description
Buffered decoder for newline-delimited JSON streams (NDJSON).
Accumulates raw bytes, splits on newline boundaries, and yields complete JSON lines. Handles the common case where a single JSON object is split across multiple network chunks.
§Example
use llm_pipeline::StreamingDecoder;
let mut decoder = StreamingDecoder::new();
// First chunk: partial JSON
let values = decoder.decode(b"{\"response\":");
assert!(values.is_empty());
// Second chunk: completes the line
let values = decoder.decode(b"\"hello\"}\n");
assert_eq!(values.len(), 1);
assert_eq!(values[0]["response"], "hello");Implementations§
Source§impl StreamingDecoder
impl StreamingDecoder
Sourcepub fn decode(&mut self, chunk: &[u8]) -> Vec<Value>
pub fn decode(&mut self, chunk: &[u8]) -> Vec<Value>
Feed a raw chunk into the decoder and return any complete JSON lines.
Each returned value is a parsed JSON Value from one complete line.
Incomplete lines are buffered until the next chunk arrives.
Sourcepub fn flush(&mut self) -> Option<Value>
pub fn flush(&mut self) -> Option<Value>
Flush remaining buffer content, attempting to parse it as JSON.
Call this after the stream ends to handle any trailing data not terminated by a newline. If direct parsing fails, attempts auto-completion of truncated JSON (closing unclosed strings, brackets, and braces).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StreamingDecoder
impl RefUnwindSafe for StreamingDecoder
impl Send for StreamingDecoder
impl Sync for StreamingDecoder
impl Unpin for StreamingDecoder
impl UnsafeUnpin for StreamingDecoder
impl UnwindSafe for StreamingDecoder
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
Mutably borrows from an owned value. Read more