Skip to main content

Overview

decodeStreamSync converts TOON-formatted lines into a stream of structured JSON events without building the complete value tree in memory. This enables memory-efficient parsing, custom transformations, and progressive processing of TOON data.
This is the synchronous variant that accepts and returns synchronous iterables. For async sources like file streams or network responses, use decodeStream instead.

Function Signature

Parameters

Iterable<string>
required
An iterable of TOON lines (without trailing newlines). Can be an array of strings, a generator, or any synchronous iterable.
DecodeStreamOptions
Optional configuration object for decoding behavior.
number
default:2
Number of spaces per indentation level. Must match the indentation used in the TOON input.
boolean
default:true
When true, enforces strict validation of array lengths, tabular row counts, and blank line restrictions.
Path expansion not supported: The expandPaths option from decode() is not available in streaming mode. Streaming decoders emit raw events without post-processing transformations.

Return Value

Iterable<JsonStreamEvent>
A synchronous 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

Basic Event Streaming

Process TOON lines event-by-event for custom handling:

Array Processing

Handle array events with declared lengths:

Custom Value Builder

Build values with custom logic by handling events manually:

Filtering Values During Decode

Use streaming events to filter or transform data on-the-fly:

Reading from a Generator

Work with lazy line generators for memory efficiency:

Custom Indentation

Decode TOON with non-standard indentation:

Strict vs. Non-Strict Mode

Control validation behavior with the strict option:

Event Stream Lifecycle

Understanding the event sequence for different TOON structures:

Performance Considerations

Memory Efficient

Only the current event is in memory at any time, enabling processing of arbitrarily large TOON files.

Early Termination

Stop iteration at any point to avoid processing unnecessary data.

No Buffering

Events are yielded as soon as they’re parsed with zero buffering overhead.

Generator-Friendly

Works seamlessly with generator functions and lazy iterables.

Error Handling

Errors are thrown synchronously when invalid TOON syntax is encountered:

decodeStream

Async variant for streaming sources like file readers and network responses

decodeFromLines

Decode lines directly to a value (supports path expansion)

decode

Standard decoder that builds the complete value tree

Event Builder Guide

Learn how to build values from event streams

Type Definitions