pub struct StreamingParser { /* private fields */ }Expand description
Streaming JSON parser for incremental parsing.
Accumulates chunks and extracts complete JSON values as they become available.
§Example
use simple_agents_healing::streaming::StreamingParser;
let mut parser = StreamingParser::new();
// Stream comes in chunks
parser.feed(r#"{"id": 1, "#);
parser.feed(r#""name": "Alice", "#);
parser.feed(r#""age": 30}"#);
// Get the complete parsed value
let result = parser.finalize().unwrap();
assert_eq!(result.value["id"], 1);
assert_eq!(result.value["name"], "Alice");
assert_eq!(result.value["age"], 30);Implementations§
Source§impl StreamingParser
impl StreamingParser
Sourcepub fn with_config(config: ParserConfig) -> Self
pub fn with_config(config: ParserConfig) -> Self
Create a streaming parser with custom configuration.
Sourcepub fn feed(&mut self, chunk: &str) -> Vec<Value>
pub fn feed(&mut self, chunk: &str) -> Vec<Value>
Feed a chunk of JSON data to the parser.
Returns a list of complete values that were extracted from this chunk. For single objects, this will be empty until the final chunk. For arrays, this can return completed array elements.
§Example
use simple_agents_healing::streaming::StreamingParser;
let mut parser = StreamingParser::new();
// Array streaming: extract complete elements
parser.feed(r#"[{"id": 1}, "#);
parser.feed(r#"{"id": 2}, "#);
let values = parser.feed(r#"{"id": 3}]"#);Sourcepub fn try_parse(&self) -> Option<CoercionResult<Value>>
pub fn try_parse(&self) -> Option<CoercionResult<Value>>
Try to parse the current buffer as a complete JSON value.
Returns Some(value) if the buffer contains a complete, parseable value.
Returns None if more data is needed or if the JSON is incomplete.
Sourcepub fn finalize(self) -> Result<CoercionResult<Value>, SimpleAgentsError>
pub fn finalize(self) -> Result<CoercionResult<Value>, SimpleAgentsError>
Finalize the stream and get the complete parsed value.
This attempts to parse the entire accumulated buffer as a single JSON value. Call this when the stream is complete.
§Errors
Returns an error if the accumulated buffer cannot be parsed as valid JSON.
§Example
use simple_agents_healing::streaming::StreamingParser;
let mut parser = StreamingParser::new();
parser.feed(r#"{"name": "#);
parser.feed(r#""Alice"}"#);
let result = parser.finalize().unwrap();
assert_eq!(result.value["name"], "Alice");Sourcepub fn buffer_len(&self) -> usize
pub fn buffer_len(&self) -> usize
Get the current buffer size in bytes.