Format Overview
TOON syntax reference with concrete examples. This page covers the complete format specification.Data Model
TOON models data the same way as JSON:- Primitives: strings, numbers, booleans, and
null - Objects: mappings from string keys to values
- Arrays: ordered sequences of values
TOON is fully lossless:
decode(encode(x)) always equals x after normalization of non-JSON types like Date, NaN, etc.Root Forms
A TOON document can represent three root forms:Root Object (Most Common)
Root Object (Most Common)
Fields appear at depth 0 with no parent key:This is the most common form for structured data.
Root Array
Root Array
Begins with Or for primitive arrays:
[N]: or [N]{fields}: at depth 0:Root Primitive
Root Primitive
A single primitive value:Or:
Objects
Simple Objects
Objects with primitive values usekey: value syntax, one field per line:
- One space follows the colon
- Indentation replaces braces
- Keys are unquoted when safe (see Quoting Rules)
Nested Objects
Nested objects add one indentation level (default: 2 spaces):When a key ends with
: and has no value on the same line, it opens a nested object. All lines at the next indentation level belong to that object.Empty Objects
An empty object at the root yields an empty document (no lines). A nested empty object iskey: alone, with no children:
{ "data": {}, "status": "ready" }.
Arrays
TOON detects array structure and chooses the most efficient representation. Arrays always declare their length in brackets:[N].
Primitive Arrays (Inline)
Arrays of primitives render inline:Tabular Arrays (Uniform Objects)
When all objects in an array share the same set of primitive-valued keys, TOON uses tabular format:items— Array key name[2]— Array length (2 rows){sku,qty,price}— Field names:— Opens the array body
- Values in the same order as field list
- Separated by the active delimiter (comma by default)
- Encoded as primitives (strings, numbers, booleans,
null)
Tabular Requirements
Tabular Requirements
An array qualifies for tabular format when:
- All elements are objects
- All objects have identical field sets (same keys, any order)
- All values are primitives (no nested arrays/objects)
Complex Tabular Example
Here’s a realistic example with various data types:- Numbers:
id,salary - Strings:
name,department,startDate - Booleans:
active
Non-Uniform Arrays (List Format)
Arrays that don’t meet tabular requirements use list format with hyphen markers:- at one indentation level deeper than the parent array header.
Objects as List Items
When array elements are objects:extra field—this non-uniformity requires list format.
Arrays of Arrays
When you have arrays containing primitive inner arrays:Empty Arrays
Empty arrays declare length zero with no elements:Array Headers
Header Syntax
Array headers follow this pattern:- N — Non-negative integer length
- delimiter (optional) — Explicitly declares the active delimiter
- fields (optional) — For tabular arrays:
{field1,field2,field3}
Delimiter Options
TOON supports three delimiters:Comma (Default)
Comma (Default)
The comma is the default delimiter and requires no special declaration:Strings containing commas must be quoted:
Tab Delimiter
Tab Delimiter
Declare tab with Benefits:
\t inside brackets:- Often tokenizes more efficiently than commas
- Strings with commas don’t need quoting
- Better for data with many punctuation marks
Pipe Delimiter
Pipe Delimiter
Declare pipe with Benefits:
| inside brackets:- Visual separation without quoting commas or tabs
- Useful for data that naturally contains both commas and tabs
Delimiter scoping: The delimiter is scoped to the array header that declares it. Nested arrays can use different delimiters.
Quoting Rules
When Strings Need Quotes
TOON quotes strings only when necessary to maximize token efficiency. A string must be quoted if it:- Is empty (
"") - Has leading or trailing whitespace
- Equals
true,false, ornull(case-sensitive) - Looks like a number (e.g.,
"42","-3.14","1e-6","05") - Contains special characters:
:,",\,[,],{,}, newline, tab, carriage return - Contains the active delimiter
- Equals
"-"or starts with"-"followed by any character
Examples: When to Quote
Examples: When to Quote
Escape Sequences
In quoted strings and keys, only five escape sequences are valid:| Character | Escape | Example |
|---|---|---|
Backslash (\) | \\ | "path\\to\\file" |
Double quote (") | \" | "She said \"hello\"" |
| Newline (U+000A) | \n | "line1\nline2" |
| Carriage return (U+000D) | \r | "text\r\n" |
| Tab (U+0009) | \t | "col1\tcol2" |
Type Conversions
TOON normalizes non-JSON types before encoding:Numbers
Numbers are emitted in canonical decimal form:Decoders accept both decimal and exponent forms on input (
42, -3.14, 1e-6).Special Values
| Input | Output |
|---|---|
NaN | null |
Infinity | null |
-Infinity | null |
undefined | null |
function | null |
symbol | null |
BigInt
Date Objects
Date objects serialize to ISO 8601 strings:Custom Serialization with toJSON
Objects with atoJSON() method are serialized by calling the method:
Key Folding (Optional)
Key folding is an optional encoder feature that collapses chains of single-key objects into dotted paths.Basic Folding
Standard nesting:keyFolding: 'safe'):
data.metadata.items.
When Folding Applies
A chain of objects is foldable when:- Each object in the chain has exactly one key
- The leaf value is a primitive, array, or empty object
- All segments are valid identifiers:
^[A-Za-z_][A-Za-z0-9_]*$ - No segment requires quoting
- The resulting key doesn’t collide with existing keys
Folding Examples
Folding Examples
Foldable:Not foldable (branch):Not foldable (invalid segment):
Round-Trip with Path Expansion
When decoding TOON that used key folding, enable path expansion to restore the nested structure:Advanced Features
Tabular Header on List Items
When a list-item object has a tabular array as its first field, the tabular header appears on the hyphen line:- Tabular rows: Two levels deeper than hyphen
- Other fields: One level deeper than hyphen
Custom Indentation
The default indentation is 2 spaces, but you can customize it:Streaming Large Datasets
For memory-efficient processing of large datasets:Next Steps
When to Use TOON
Learn when TOON excels and when to use alternatives
Quick Start
Install the library and start encoding your data
API Reference
Explore encoding and decoding options
Advanced Options
Configuration options and fine-tuning
