Skip to main content

PartialJsonParser

Struct PartialJsonParser 

Source
pub struct PartialJsonParser { /* private fields */ }
Expand description

Incremental JSON parser that can handle partial/incomplete JSON.

Builds up a string token by token and attempts to parse at each step, returning the best partial result possible. This is designed for streaming LLM output where JSON arrives in small chunks and may be incomplete until the stream finishes.

§Strategy

  1. Accumulate tokens into an internal buffer.
  2. On each push_and_parse, attempt to parse the buffer as complete JSON.
  3. If that fails, try to repair the partial JSON by closing unclosed brackets/braces and truncating incomplete string values.
  4. If repair yields valid JSON, return it; otherwise return PartialJsonError::Incomplete.

§Example

let mut parser = PartialJsonParser::new();
// Simulating token-by-token LLM output
let _ = parser.push_and_parse(r#"{"name":"#); // Incomplete
let v = parser.push_and_parse(r#""Alice","age":30}"#); // Ok({"name":"Alice","age":30})

Implementations§

Source§

impl PartialJsonParser

Source

pub fn new() -> Self

Create a new, empty parser.

Source

pub fn push_and_parse(&mut self, token: &str) -> Result<Value, PartialJsonError>

Push a new token and attempt to parse the accumulated buffer.

Returns Ok(value) if the buffer (after optional repair) yields valid JSON, or Err(PartialJsonError::Incomplete) if it does not yet form any parseable JSON.

Source

pub fn finalize(self) -> Result<Value, PartialJsonError>

Get the final complete value.

Call this when the stream has ended. It first tries to parse the fence-stripped buffer, then falls back to the repaired version.

Source

pub fn buffer(&self) -> &str

Return a reference to the current buffer contents.

Source

pub fn is_in_string(&self) -> bool

Whether the parser is currently inside a JSON string.

Source

pub fn depth(&self) -> usize

Current nesting depth of brackets/braces.

Trait Implementations§

Source§

impl Default for PartialJsonParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more