Skip to main content

Overview

The TOON SDK provides streaming APIs for processing large datasets without loading everything into memory. This is essential for handling files larger than available RAM, processing data in real-time, or building efficient data pipelines.

Streaming Encoding

Basic Streaming Encode

The encodeLines() function yields TOON lines one at a time:

Writing to Files

Stream directly to files without memory overhead:

Streaming to stdout

Streaming HTTP Responses

Streaming with Options

encodeLines() returns an iterable (not an array). You can convert to array with Array.from() if needed, but this defeats the memory efficiency.

Streaming Decoding

Synchronous Streaming Decode

The decodeStreamSync() function yields JSON events without building the full value tree:

Event Types

Streaming decoder emits these event types:

Processing Events

Async Streaming Decode

The decodeStream() function works with async iterables:

Filtering During Stream Decode

Path expansion (expandPaths: 'safe') is not supported in streaming mode. Use the standard decode() or decodeFromLines() functions if you need path expansion.

Building Values from Events

The SDK provides an internal function to build values from events, but you can implement your own:

Streaming Options

Both sync and async streaming decoders accept options:
The expandPaths option is not available in streaming mode. Stream decoding focuses on low-level event processing without post-processing transformations.

Real-World Use Cases

ETL Pipeline

Memory-Efficient Aggregation

Streaming Transformation

Performance Comparison

Use streaming APIs when:
  • Dataset exceeds 50% of available memory
  • Processing data from network streams
  • Building ETL pipelines
  • Implementing real-time processing

API Reference

encodeLines(input, options?)

Encodes a JavaScript value as an iterable of TOON lines. Parameters:
  • input: unknown - Any JavaScript value
  • options?: EncodeOptions - Optional configuration
Returns: Iterable<string> - Iterable of TOON lines (without newlines) Source: packages/toon/src/index.ts:99

decodeStreamSync(lines, options?)

Synchronously decodes TOON lines into JSON events. Parameters:
  • lines: Iterable<string> - Iterable of TOON lines (without newlines)
  • options?: DecodeStreamOptions - Optional configuration (no expandPaths)
Returns: Iterable<JsonStreamEvent> - Iterable of JSON events Source: packages/toon/src/index.ts:175

decodeStream(source, options?)

Asynchronously decodes TOON lines into JSON events. Parameters:
  • source: AsyncIterable<string> | Iterable<string> - Async or sync iterable of lines
  • options?: DecodeStreamOptions - Optional configuration (no expandPaths)
Returns: AsyncIterable<JsonStreamEvent> - Async iterable of JSON events Source: packages/toon/src/index.ts:208

decodeFromLines(lines, options?)

Decodes TOON format from pre-split lines into a JavaScript value. Parameters:
  • lines: Iterable<string> - Iterable of TOON lines (without newlines)
  • options?: DecodeOptions - Optional configuration (supports expandPaths)
Returns: JsonValue - Parsed JavaScript value Source: packages/toon/src/index.ts:129
1

Choose streaming API

Use encodeLines() for encoding or decodeStream() / decodeStreamSync() for decoding.
2

Process incrementally

Handle lines or events one at a time without building full dataset in memory.
3

Configure options

Set indentation, validation, and other options as needed.
4

Handle errors

Implement error handling for malformed data or I/O errors.

Best Practices

Stream for large data

Use streaming APIs for datasets over 50MB or when memory is limited.

Process incrementally

Handle events as they arrive instead of buffering everything.

Validate early

Enable strict mode to catch errors early in the stream.

Handle backpressure

Use async streaming with proper flow control for network streams.

Next Steps

Encoding Guide

Learn all encoding options and transformations

Decoding Guide

Master TOON parsing with validation and path expansion

CLI Usage

Use command-line tools for batch processing