Skip to main content
TOON provides extensive configuration options for encoding and decoding. This page documents all available options with their types, defaults, and use cases.

Encoding Options

Configure how JavaScript values are encoded into TOON format.

indent

number
default:"2"
Number of spaces per indentation level.
Use cases:
  • 2 - Human-readable default
  • 4 - Match JSON or YAML conventions
  • 0 - Maximum token efficiency

delimiter

',' | '\t' | '|'
default:"','"
Delimiter character for tabular array rows and inline primitive arrays.
Token efficiency:
  • Tab (\t) - Most efficient (~5-10% fewer tokens)
  • Comma (,) - Default, human-readable
  • Pipe (|) - Alternative for comma-heavy data
Use DELIMITERS.tab for production LLM inputs where token count is critical.

keyFolding

'off' | 'safe'
default:"'off'"
Enable key folding to collapse single-key wrapper chains into dotted paths.
Behavior:
  • '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

number
default:"Infinity"
Maximum number of segments to fold when keyFolding is enabled.
Use cases:
  • Infinity - Fold entire chains (default)
  • 2-5 - Limit folding depth for readability
  • 0 or 1 - Effectively disables folding

replacer

function
default:"undefined"
A function to transform or filter values during encoding.
Type signature:
Basic example:
See Replacer Function for comprehensive documentation and examples.

Decoding Options

Configure how TOON strings are decoded into JavaScript values.

indent

number
default:"2"
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

boolean
default:"true"
Enable strict validation of array lengths and tabular row counts.
Validation checks:
  • Array declared length [N] matches actual row count
  • Tabular row field counts match header {fields}
  • All array elements are present
Use strict: true (default) in production to catch corrupted or truncated data. Disable only for testing or when processing known-good inputs.

expandPaths

'off' | 'safe'
default:"'off'"
Enable path expansion to reconstruct dotted keys into nested objects.
Behavior:
  • '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:
Example:
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 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
  • replacer return values are re-normalized

Decoding Validation

  • Indentation must be consistent
  • Array lengths [N] must match row counts (when strict: true)
  • Tabular rows must match field count in {fields} header
  • Quoted strings must be properly escaped
  • Primitives must be valid (booleans, numbers, null)
Use strict: true (default) to catch malformed TOON data early. Disable only when processing known-good inputs or during development.

Performance Impact

Encoding Options Impact

Decoding Options Impact

For large datasets, use streaming APIs (encodeLines, decodeStream) regardless of options to minimize memory usage.

Examples

See also: