pub struct JsonStreamBuffer { /* private fields */ }Expand description
Incremental JSON stream parser for buffering and parsing newline-delimited JSON.
Accumulates input chunks and attempts to parse complete JSON values. Handles cases where JSON objects span multiple lines or chunks.
§Buffer overflow protection
If the buffer exceeds max_buffer_size bytes, a CLIJSONDecodeError is returned
and the buffer is cleared.
Implementations§
Source§impl JsonStreamBuffer
impl JsonStreamBuffer
Sourcepub fn new(max_buffer_size: usize) -> Self
pub fn new(max_buffer_size: usize) -> Self
Creates a new JsonStreamBuffer with the given maximum buffer size.
§Example
use claude_code::JsonStreamBuffer;
let _buffer = JsonStreamBuffer::new(1024 * 1024);Sourcepub fn push_chunk(
&mut self,
chunk: &str,
) -> Result<Vec<Value>, CLIJSONDecodeError>
pub fn push_chunk( &mut self, chunk: &str, ) -> Result<Vec<Value>, CLIJSONDecodeError>
Pushes a chunk of data into the buffer and returns any complete JSON values.
The chunk is split by newlines, and each line is appended to the internal buffer. After each line, the buffer is tested for valid JSON. If it parses successfully, the value is collected and the buffer is cleared for the next message.
§Returns
A Vec<Value> of all complete JSON values parsed from this chunk.
§Errors
Returns CLIJSONDecodeError if the buffer exceeds the maximum size.
§Example
use claude_code::JsonStreamBuffer;
let mut buffer = JsonStreamBuffer::new(1024);
let parsed = buffer.push_chunk("{\"type\":\"system\"}\n").unwrap();
assert_eq!(parsed.len(), 1);Trait Implementations§
Source§impl Clone for JsonStreamBuffer
impl Clone for JsonStreamBuffer
Source§fn clone(&self) -> JsonStreamBuffer
fn clone(&self) -> JsonStreamBuffer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more