Encoding Options
Configure how JavaScript values are encoded into TOON format.indent
Number of spaces per indentation level.
2- Human-readable default4- Match JSON or YAML conventions0- Maximum token efficiency
delimiter
Delimiter character for tabular array rows and inline primitive arrays.
- Tab (
\t) - Most efficient (~5-10% fewer tokens) - Comma (
,) - Default, human-readable - Pipe (
|) - Alternative for comma-heavy data
keyFolding
Enable key folding to collapse single-key wrapper chains into dotted paths.
'off'- No folding (default)'safe'- Fold single-key chains into dotted paths (e.g.,a.b.c)- Only folds when all segments are valid identifiers
- Stops at multi-key objects, arrays, or primitives
- Avoids collisions with literal dotted keys
When encoding with
keyFolding: 'safe', decode with expandPaths: 'safe' to reconstruct the original nested structure.flattenDepth
Maximum number of segments to fold when
keyFolding is enabled.Infinity- Fold entire chains (default)2-5- Limit folding depth for readability0or1- Effectively disables folding
replacer
A function to transform or filter values during encoding.
Decoding Options
Configure how TOON strings are decoded into JavaScript values.indent
Number of spaces per indentation level (must match encoding).
The
indent option must match the indentation used during encoding. Mismatched indentation will cause parse errors.strict
Enable strict validation of array lengths and tabular row counts.
- Array declared length
[N]matches actual row count - Tabular row field counts match header
{fields} - All array elements are present
expandPaths
Enable path expansion to reconstruct dotted keys into nested objects.
'off'- Treat dotted keys as literals (default)'safe'- Expand dotted keys into nested objects- Only expands when all segments are valid identifiers
- Pairs with
keyFolding: 'safe'for lossless round-trips
Path expansion is not supported in streaming mode (
decodeStream, decodeStreamSync). Use decodeFromLines for path expansion with pre-split lines.Streaming Options
Options for streaming decode operations.DecodeStreamOptions
Streaming decode supports a subset of DecodeOptions:
Why no
expandPaths in streaming? Path expansion requires buffering the full object tree to resolve nested structures. Streaming decode yields events incrementally without building the tree.Type Definitions
Full TypeScript type definitions:Default Values
All options with their defaults:| Option | Type | Default | Context |
|---|---|---|---|
indent | number | 2 | Encode, Decode |
delimiter | Delimiter | ',' | Encode |
keyFolding | 'off' | 'safe' | 'off' | Encode |
flattenDepth | number | Infinity | Encode |
replacer | function | undefined | Encode |
strict | boolean | true | Decode |
expandPaths | 'off' | 'safe' | 'off' | Decode (non-streaming) |
Option Combinations
Maximum Token Efficiency
Human-Readable Output
Round-Trip with Key Folding
Lenient Parsing
Validation
TOON validates inputs during encoding/decoding:Encoding Validation
- All values are normalized to JSON data model
- Non-JSON types (Date, BigInt, etc.) are converted
- Circular references cause errors
replacerreturn values are re-normalized
Decoding Validation
- Indentation must be consistent
- Array lengths
[N]must match row counts (whenstrict: true) - Tabular rows must match field count in
{fields}header - Quoted strings must be properly escaped
- Primitives must be valid (booleans, numbers, null)
Performance Impact
Encoding Options Impact
| Option | Performance Impact | Token Impact |
|---|---|---|
indent: 0 | None | −3-5% tokens |
delimiter: tab | None | −5-10% tokens |
keyFolding: 'safe' | Minimal | −20-40% tokens (nested data) |
replacer | Moderate (per-value) | Varies |
Decoding Options Impact
| Option | Performance Impact |
|---|---|
strict: true | Minimal (validation overhead) |
expandPaths: 'safe' | Moderate (tree reconstruction) |
For large datasets, use streaming APIs (
encodeLines, decodeStream) regardless of options to minimize memory usage.Examples
See also:- Performance Guide - Optimization strategies
- Replacer Function - Detailed replacer examples
- API Reference - Complete API documentation
