JsonStreamParser class

A streaming JSON parser optimized for LLM responses.

This parser processes JSON data character-by-character as it streams in, allowing you to react to properties before the entire JSON is received. It's specifically designed for handling Large Language Model (LLM) streaming responses that output structured JSON data.

Key Features

  • Reactive property access: Subscribe to JSON properties as they complete
  • Streaming string values: Receive string content chunk-by-chunk as it arrives
  • Path-based subscriptions: Access nested properties with dot notation or chainable API
  • Type safety: Typed property streams for all JSON types
  • Dynamic list handling: React to array elements as soon as they start arriving

Basic Usage

final parser = JsonStreamParser(streamFromLLM);

// Subscribe to properties
parser.getStringProperty('title').stream.listen((chunk) {
  print('Title chunk: $chunk');
});

// Wait for complete values
final age = await parser.getNumberProperty('user.age').future;
print('Age: $age');

Path Syntax

Access nested properties using dot notation:

  • 'title' - Root property
  • 'user.name' - Nested property
  • 'items[0].name' - Array element property
  • 'data.users[2].profile.age' - Deep nesting

Disposal

Call dispose when done to clean up resources:

await parser.dispose();

Constructors

JsonStreamParser(Stream<String> stream)
Creates a new JSON stream parser that processes the given stream.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose() Future<void>
Disposes the parser and cleans up all resources.
getBooleanProperty(String propertyPath) BooleanPropertyStream
Gets a stream for a boolean property at the specified propertyPath.
getListProperty<E extends Object?>(String propertyPath, {void onElement(PropertyStream propertyStream, int index)?}) ListPropertyStream<E>
Gets a stream for a list (array) property at the specified propertyPath.
getMapProperty(String propertyPath) MapPropertyStream
Gets a stream for a map (object) property at the specified propertyPath.
getNullProperty(String propertyPath) NullPropertyStream
Gets a stream for a null property at the specified propertyPath.
getNumberProperty(String propertyPath) NumberPropertyStream
Gets a stream for a number property at the specified propertyPath.
getStringProperty(String propertyPath) StringPropertyStream
Gets a stream for a string property at the specified propertyPath.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited