pub struct StreamingJsonParser<T> { /* private fields */ }Expand description
Parses JSON objects of type T from a stream of text chunks.
It scans for objects inside a JSON array (by default) and yields each fully-formed object as soon as braces are balanced. Useful when an LLM streams a JSON plan and you want to display / process steps incrementally.
Implementations§
Source§impl<T> StreamingJsonParser<T>where
T: DeserializeOwned + Clone,
impl<T> StreamingJsonParser<T>where
T: DeserializeOwned + Clone,
pub fn new() -> StreamingJsonParser<T>
Sourcepub fn with_key(self, key: impl Into<String>) -> StreamingJsonParser<T>
pub fn with_key(self, key: impl Into<String>) -> StreamingJsonParser<T>
Set the array key to look for. e.g. with_key("steps") will look for
"steps":[...] in the JSON.
Sourcepub fn process_chunk(&mut self, chunk: &str) -> Vec<T>
pub fn process_chunk(&mut self, chunk: &str) -> Vec<T>
Append a new chunk and return any newly parsed items.
Sourcepub fn accumulated(&self) -> &[T]
pub fn accumulated(&self) -> &[T]
Return all accumulated items so far.
Sourcepub fn into_buffer(self) -> String
pub fn into_buffer(self) -> String
Consume parser and return the full raw text.
Trait Implementations§
Source§impl<T> Debug for StreamingJsonParser<T>where
T: Debug,
impl<T> Debug for StreamingJsonParser<T>where
T: Debug,
Source§impl<T> Default for StreamingJsonParser<T>where
T: DeserializeOwned + Clone,
impl<T> Default for StreamingJsonParser<T>where
T: DeserializeOwned + Clone,
Source§fn default() -> StreamingJsonParser<T>
fn default() -> StreamingJsonParser<T>
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl<T> Freeze for StreamingJsonParser<T>
impl<T> RefUnwindSafe for StreamingJsonParser<T>where
T: RefUnwindSafe,
impl<T> Send for StreamingJsonParser<T>where
T: Send,
impl<T> Sync for StreamingJsonParser<T>where
T: Sync,
impl<T> Unpin for StreamingJsonParser<T>where
T: Unpin,
impl<T> UnsafeUnpin for StreamingJsonParser<T>
impl<T> UnwindSafe for StreamingJsonParser<T>where
T: UnwindSafe,
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