Overview
decodeStream converts TOON-formatted lines into a stream of structured JSON events without building the complete value tree in memory. This async variant supports both synchronous and asynchronous line sources, making it ideal for file streams, network responses, or any async data source.
This function accepts both async and sync iterables and always returns an async iterable. For purely synchronous sources, you can use
decodeStreamSync for better performance.Function Signature
Parameters
AsyncIterable<string> | Iterable<string>
required
An iterable of TOON lines (without trailing newlines). Accepts:
- Async iterables (e.g.,
ReadableStream, async generators) - Sync iterables (e.g., arrays, sync generators)
DecodeStreamOptions
Return Value
AsyncIterable<JsonStreamEvent>
An async iterable that yields JSON stream events. Each event represents a structural element of the JSON data model.
Event Types
The decoder emits six types of events:{ type: "startObject" }
Marks the beginning of a JSON object.
{ type: "endObject" }
Marks the end of a JSON object.
{ type: "startArray", length: number }
Marks the beginning of a JSON array. The
length field indicates the declared array length from the TOON header.{ type: "endArray" }
Marks the end of a JSON array.
{ type: "key", key: string, wasQuoted?: boolean }
Represents an object key. The optional
wasQuoted field is true when the key was quoted in the TOON source.{ type: "primitive", value: JsonPrimitive }
Represents a primitive value (string, number, boolean, or null).
Usage Examples
Async File Stream Processing
Process large TOON files line-by-line without loading into memory:HTTP Response Streaming
Decode TOON data from a streaming HTTP response:Async Generator Source
Use with async generators for custom data sources:Progressive Data Processing
Build and emit partial results as data arrives:Sync Source Compatibility
Works seamlessly with synchronous sources too:Building Values from Events
Reconstruct the full value tree from events:For standard value reconstruction, use
decodeFromLines() which handles this automatically and supports path expansion.Error Handling with Async Sources
Handle errors gracefully in async streaming contexts:Early Termination
Stop processing when a condition is met:Sync vs Async Variants
Key differences betweendecodeStream and decodeStreamSync:
decodeStream
- Accepts async or sync sources
- Returns
AsyncIterable - Use
for await...ofloop - Ideal for I/O operations
decodeStreamSync
- Accepts only sync sources
- Returns
Iterable - Use
for...ofloop - Better performance for sync data
Performance Considerations
Memory Efficient
Processes data incrementally without loading the entire structure into memory.
Backpressure Handling
Respects async iterator backpressure for controlled resource usage.
Lazy Evaluation
Events are generated on-demand as you iterate, enabling early termination.
I/O Optimized
Designed for file streams, network responses, and other async sources.
Custom Options
Configure indentation and validation:Streaming from Network
Real-world example with fetch and stream processing:Error Handling
Comprehensive error handling patterns:Related Functions
decodeStreamSync
Synchronous variant for sync-only sources with better performance
decodeFromLines
Decode lines directly to a value (supports path expansion)
decode
Standard decoder that builds the complete value tree
Streaming Guide
Complete guide to streaming with TOON
Type Definitions
Best Practices
1
Choose the right variant
Use
decodeStream for async sources (files, network), decodeStreamSync for sync arrays/generators2
Handle backpressure
Process events as they arrive rather than buffering to maintain memory efficiency
3
Validate early
Check event types and values as you process them to fail fast on invalid data
4
Clean up resources
Use try-finally blocks to ensure async iterators are properly closed
