Skip to main content

Function Signature

function decode(input: string, options?: DecodeOptions): JsonValue
Decodes a TOON format string into a JavaScript value.

Parameters

input
string
required
TOON formatted string to decode.
options
DecodeOptions
Optional decoding configuration.
indent
number
default:"2"
Number of spaces per indentation level. Must match the indentation used when encoding.
strict
boolean
default:"true"
When true, enforce strict validation of array lengths and tabular row counts.
expandPaths
'off' | 'safe'
default:"'off'"
Enable path expansion to reconstruct dotted keys into nested objects. When set to 'safe', keys containing dots are expanded into nested structures if all segments are valid identifiers (e.g., data.metadata.items becomes nested objects). Pairs with keyFolding='safe' for lossless round-trips.

Returns

result
JsonValue
Parsed JavaScript value (object, array, or primitive).
type JsonValue = JsonPrimitive | JsonObject | JsonArray
type JsonPrimitive = string | number | boolean | null
type JsonObject = { [Key in string]: JsonValue }
type JsonArray = JsonValue[]

Examples

import { decode } from 'toon';

const toonString = 'name: Alice\nage: 30';
const result = decode(toonString);
console.log(result);
// Output: { name: 'Alice', age: 30 }

Error Handling

The decode function throws errors for invalid TOON format:
  • Indentation errors (incorrect nesting)
  • Array length mismatches (in strict mode)
  • Invalid syntax (malformed headers, values, etc.)
  • Tabular row count mismatches (in strict mode)
import { decode } from 'toon';

try {
  const result = decode('invalid toon format');
} catch (error) {
  console.error('Decode failed:', error.message);
}

Build docs developers (and LLMs) love