You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both encoding and decoding operations use streaming output, writing incrementally without building the full output string in memory. This makes the CLI efficient for large datasets without requiring additional configuration.
110
110
111
-
**JSON → TOON (Encode)**
112
-
- Streams TOON lines to output
113
-
- No full TOON string in memory
111
+
**JSON → TOON (Encode)**:
114
112
115
-
**TOON → JSON (Decode)**
116
-
- Streams JSON tokens to output
117
-
- No full JSON string in memory
113
+
- Streams TOON lines to output.
114
+
- No full TOON string in memory.
115
+
116
+
**TOON → JSON (Decode)**:
117
+
118
+
- Uses the same event-based streaming decoder as the `decodeStream` API in `@toon-format/toon`.
119
+
- Streams JSON tokens to output.
120
+
- No full JSON string in memory.
121
+
- When `--expand-paths safe` is enabled, falls back to non-streaming decode internally to apply deep-merge expansion before writing JSON.
122
+
123
+
Process large files with minimal memory usage:
118
124
119
125
```bash
120
-
# Encode large JSON file with minimal memory usage
126
+
# Encode large JSON file
121
127
toon huge-dataset.json -o output.toon
122
128
123
-
# Decode large TOON file with minimal memory usage
129
+
# Decode large TOON file
124
130
toon huge-dataset.toon -o output.json
125
131
126
132
# Process millions of records efficiently via stdin
Now that you've seen your first TOON document, read the [Format Overview](/guide/format-overview) for complete syntax details (objects, arrays, quoting rules, key folding), then explore [Using TOON with LLMs](/guide/llm-prompts) to see how to use it effectively in prompts. For implementation details, check the [API reference](/reference/api) (TypeScript) or the [specification](/reference/spec) (language-agnostic normative rules).
240
+
241
+
For large datasets or streaming use-cases, see `encodeLines`, `decodeFromLines`, and `decodeStream` in the [API reference](/reference/api).
This streaming approach prevents out-of-memory errors when preparing large context windows for LLMs. For complete details on `encodeLines()`, see the [API reference](/reference/api#encodelines).
120
120
121
+
**Consuming streaming LLM outputs:** If your LLM client exposes streaming text and you buffer by lines, you can decode TOON incrementally:
For streaming decode APIs, see [`decodeFromLines()`](/reference/api#decodeFromLines-lines-options) and [`decodeStream()`](/reference/api#decodeStream-source-options).
145
+
121
146
## Tips and Pitfalls
122
147
123
148
**Show, don't describe.** Don't explain TOON syntax in detail – just show an example. Models learn the pattern from context. A simple code block with 2-5 rows is more effective than paragraphs of explanation.
Decodes TOON format from pre-split lines into a JavaScript value. This is a streaming-friendly wrapper around the event-based decoder that builds the full value in memory.
306
+
307
+
Useful when you already have lines as an array or iterable (e.g., from file streams, readline interfaces, or network responses) and want the standard decode behavior with path expansion support.
308
+
309
+
### Parameters
310
+
311
+
| Parameter | Type | Description |
312
+
|-----------|------|-------------|
313
+
|`lines`|`Iterable<string>`| Iterable of TOON lines (without trailing newlines) |
314
+
|`options`|`DecodeOptions?`| Optional decoding configuration (see below) |
315
+
316
+
### Options
317
+
318
+
| Option | Type | Default | Description |
319
+
|--------|------|---------|-------------|
320
+
|`indent`|`number`|`2`| Expected number of spaces per indentation level |
const value =decodeFromLines(lines, { expandPaths: 'safe' })
361
+
// { user: { name: 'Alice', age: 30 } }
362
+
```
363
+
364
+
## `decodeStreamSync(lines, options?)`
365
+
366
+
Synchronously decodes TOON lines into a stream of JSON events. This function yields structured events that represent the JSON data model without building the full value tree.
367
+
368
+
Useful for streaming processing, custom transformations, or memory-efficient parsing of large datasets where you don't need the full value in memory.
369
+
370
+
::: info Event Streaming
371
+
This is a low-level API that returns individual parse events. For most use cases, [`decodeFromLines()`](#decodeFromLines-lines-options) or [`decode()`](#decode-input-options) are more convenient.
372
+
373
+
Path expansion (`expandPaths: 'safe'`) is **not supported** in streaming mode since it requires the full value tree.
374
+
:::
375
+
376
+
### Parameters
377
+
378
+
| Parameter | Type | Description |
379
+
|-----------|------|-------------|
380
+
|`lines`|`Iterable<string>`| Iterable of TOON lines (without trailing newlines) |
381
+
|`options`|`DecodeStreamOptions?`| Optional streaming decoding configuration (see below) |
382
+
383
+
### Options
384
+
385
+
| Option | Type | Default | Description |
386
+
|--------|------|---------|-------------|
387
+
|`indent`|`number`|`2`| Expected number of spaces per indentation level |
Asynchronously decodes TOON lines into a stream of JSON events. This is the async version of [`decodeStreamSync()`](#decodeStreamSync-lines-options), supporting both synchronous and asynchronous iterables.
451
+
452
+
Useful for processing file streams, network responses, or other async sources where you want to handle data incrementally as it arrives.
453
+
454
+
### Parameters
455
+
456
+
| Parameter | Type | Description |
457
+
|-----------|------|-------------|
458
+
|`source`|`AsyncIterable<string>`\|`Iterable<string>`| Async or sync iterable of TOON lines (without trailing newlines) |
459
+
|`options`|`DecodeStreamOptions?`| Optional streaming decoding configuration (see below) |
460
+
461
+
### Options
462
+
463
+
| Option | Type | Default | Description |
464
+
|--------|------|---------|-------------|
465
+
|`indent`|`number`|`2`| Expected number of spaces per indentation level |
-**Encode (JSON → TOON)**: Streams TOON lines to output without full string in memory
137
-
-**Decode (TOON → JSON)**: Streams JSON tokens to output without full string in memory
137
+
-**Decode (TOON → JSON)**: Uses the same event-based streaming decoder as the `decodeStream` API in `@toon-format/toon`, streaming JSON tokens to output without full string in memory
138
138
- Peak memory usage scales with data depth, not total size
139
+
- When `--expand-paths safe` is enabled, decode falls back to non-streaming mode internally to apply deep-merge expansion before writing JSON
139
140
140
141
> [!NOTE]
141
142
> When using `--stats` with encode, the full output string is kept in memory for token counting. Omit `--stats` for maximum memory efficiency with very large datasets.
0 commit comments