toon_x_json 0.0.4 copy "toon_x_json: ^0.0.4" to clipboard
toon_x_json: ^0.0.4 copied to clipboard

TOON <--> JSON, TOON- A compact JSON format

Token-Oriented Object Notation (TOON) for Dart #

pub package pub points License: MIT

Token-Oriented Object Notation is a compact, human-readable serialization format designed for passing structured data to Large Language Models with significantly reduced token usage. It's intended for LLM input as a lossless, drop-in representation of JSON data.

TOON's sweet spot is uniform arrays of objects – multiple fields per row, same structure across items. It borrows YAML's indentation-based structure for nested objects and CSV's tabular format for uniform data rows, then optimizes both for token efficiency in LLM contexts. For deeply nested or non-uniform data, JSON may be more efficient.

TOON achieves CSV-like compactness while adding explicit structure that helps LLMs parse and validate data reliably.

Tip

Think of TOON as a translation layer: use JSON programmatically, convert to TOON for LLM input.

Table of Contents #

Why TOON? #

AI is becoming cheaper and more accessible, but larger context windows allow for larger data inputs as well. LLM tokens still cost money – and standard JSON is verbose and token-expensive:

{
  "users": [
    { "id": 1, "name": "Alice", "role": "admin" },
    { "id": 2, "name": "Bob", "role": "user" }
  ]
}

TOON conveys the same information with fewer tokens:

users[2]{id,name,role}:
  1,Alice,admin
  2,Bob,user
Why create a new format?

For small payloads, JSON/CSV/YAML work fine. TOON's value emerges at scale: when you're making hundreds of LLM calls with uniform tabular data, eliminating repeated keys compounds savings significantly. If token costs matter to your use case, TOON reduces them. If not, stick with what works.

When NOT to use TOON

TOON excels with uniform arrays of objects, but there are cases where other formats are better:

  • Deeply nested or non-uniform structures (tabular eligibility β‰ˆ 0%): JSON-compact often uses fewer tokens. Example: complex configuration objects with many nested levels.
  • Semi-uniform arrays (~40–60% tabular eligibility): Token savings diminish. Prefer JSON if your pipelines already rely on it.
  • Flat CSV use-cases: CSV is smaller than TOON for pure tabular data. TOON adds minimal overhead (~5-10%) to provide structure (length markers, field headers, delimiter scoping) that improves LLM reliability.

See benchmarks for concrete comparisons across different data structures.

Key Features #

  • πŸ’Έ Token-efficient: typically 30-60% fewer tokens on large uniform arrays vs formatted JSON1
  • 🀿 LLM-friendly guardrails: explicit lengths and fields enable validation
  • 🍱 Minimal syntax: removes redundant punctuation (braces, brackets, most quotes)
  • πŸ“ Indentation-based structure: like YAML, uses whitespace instead of braces
  • 🧺 Tabular arrays: declare keys once, stream data as rows

Benchmarks #

Tip

Try the interactive Format Tokenization Playground to compare token usage across CSV, JSON, YAML, and TOON with your own data.

Benchmarks are organized into two tracks to ensure fair comparisons:

  • Mixed-Structure Track: Datasets with nested or semi-uniform structures (TOON vs JSON, YAML, XML). CSV excluded as it cannot properly represent these structures.
  • Flat-Only Track: Datasets with flat tabular structures where CSV is applicable (CSV vs TOON vs JSON, YAML, XML).

Token Efficiency #

Token counts are measured using the GPT-5 o200k_base tokenizer via gpt-tokenizer. Savings are calculated against formatted JSON (2-space indentation) as the primary baseline, with additional comparisons to compact JSON (minified), YAML, and XML. Actual savings vary by model and tokenizer.

The benchmarks test datasets across different structural patterns (uniform, semi-uniform, nested, deeply nested) to show where TOON excels and where other formats may be better.

Mixed-Structure Track

Datasets with nested or semi-uniform structures. CSV excluded as it cannot properly represent these structures.

πŸ›’ E-commerce orders with nested structures  β”Š  Tabular: 33%
   β”‚
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘    72,771 tokens
   β”œβ”€ vs JSON          (βˆ’33.1%)               108,806 tokens
   β”œβ”€ vs JSON compact  (+5.5%)                 68,975 tokens
   β”œβ”€ vs YAML          (βˆ’14.2%)                84,780 tokens
   └─ vs XML           (βˆ’40.5%)               122,406 tokens

🧾 Semi-uniform event logs  β”Š  Tabular: 50%
   β”‚
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘   153,211 tokens
   β”œβ”€ vs JSON          (βˆ’15.0%)               180,176 tokens
   β”œβ”€ vs JSON compact  (+19.9%)               127,731 tokens
   β”œβ”€ vs YAML          (βˆ’0.8%)                154,505 tokens
   └─ vs XML           (βˆ’25.2%)               204,777 tokens

🧩 Deeply nested configuration  β”Š  Tabular: 0%
   β”‚
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘       631 tokens
   β”œβ”€ vs JSON          (βˆ’31.3%)                   919 tokens
   β”œβ”€ vs JSON compact  (+11.9%)                   564 tokens
   β”œβ”€ vs YAML          (βˆ’6.2%)                    673 tokens
   └─ vs XML           (βˆ’37.4%)                 1,008 tokens

──────────────────────────────────── Total ────────────────────────────────────
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘   226,613 tokens
   β”œβ”€ vs JSON          (βˆ’21.8%)               289,901 tokens
   β”œβ”€ vs JSON compact  (+14.9%)               197,270 tokens
   β”œβ”€ vs YAML          (βˆ’5.6%)                239,958 tokens
   └─ vs XML           (βˆ’31.0%)               328,191 tokens

Flat-Only Track

Datasets with flat tabular structures where CSV is applicable.

πŸ‘₯ Uniform employee records  β”Š  Tabular: 100%
   β”‚
   CSV                 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘    46,954 tokens
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ    49,831 tokens   (+6.1% vs CSV)
   β”œβ”€ vs JSON          (βˆ’60.7%)               126,860 tokens
   β”œβ”€ vs JSON compact  (βˆ’36.8%)                78,856 tokens
   β”œβ”€ vs YAML          (βˆ’50.0%)                99,706 tokens
   └─ vs XML           (βˆ’66.0%)               146,444 tokens

πŸ“ˆ Time-series analytics data  β”Š  Tabular: 100%
   β”‚
   CSV                 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘     8,388 tokens
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ     9,120 tokens   (+8.7% vs CSV)
   β”œβ”€ vs JSON          (βˆ’59.0%)                22,250 tokens
   β”œβ”€ vs JSON compact  (βˆ’35.8%)                14,216 tokens
   β”œβ”€ vs YAML          (βˆ’48.9%)                17,863 tokens
   └─ vs XML           (βˆ’65.7%)                26,621 tokens

⭐ Top 100 GitHub repositories  β”Š  Tabular: 100%
   β”‚
   CSV                 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘     8,513 tokens
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ     8,745 tokens   (+2.7% vs CSV)
   β”œβ”€ vs JSON          (βˆ’42.3%)                15,145 tokens
   β”œβ”€ vs JSON compact  (βˆ’23.7%)                11,455 tokens
   β”œβ”€ vs YAML          (βˆ’33.4%)                13,129 tokens
   └─ vs XML           (βˆ’48.8%)                17,095 tokens

──────────────────────────────────── Total ────────────────────────────────────
   CSV                 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘    63,855 tokens
   TOON                β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ    67,696 tokens   (+6.0% vs CSV)
   β”œβ”€ vs JSON          (βˆ’58.8%)               164,255 tokens
   β”œβ”€ vs JSON compact  (βˆ’35.2%)               104,527 tokens
   β”œβ”€ vs YAML          (βˆ’48.2%)               130,698 tokens
   └─ vs XML           (βˆ’64.4%)               190,160 tokens

Retrieval Accuracy #

Benchmarks test LLM comprehension across different input formats using 209 data retrieval questions on 4 models.

Efficiency Ranking (Accuracy per 1K Tokens)

Each format's overall performance, balancing accuracy against token cost:

TOON           β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“   26.9  β”‚  73.9% acc  β”‚  2,744 tokens
JSON compact   β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–‘β–‘β–‘   22.9  β”‚  70.7% acc  β”‚  3,081 tokens
YAML           β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–‘β–‘β–‘β–‘β–‘β–‘   18.6  β”‚  69.0% acc  β”‚  3,719 tokens
JSON           β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘   15.3  β”‚  69.7% acc  β”‚  4,545 tokens
XML            β–“β–“β–“β–“β–“β–“β–“β–“β–“β–“β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘   13.0  β”‚  67.1% acc  β”‚  5,167 tokens

TOON achieves 73.9% accuracy (vs JSON's 69.7%) while using 39.6% fewer tokens.

Installation & Quick Start #

Add toon_x_json to your pubspec.yaml:

dependencies:
  toon_x_json: ^0.0.3

Then run:

dart pub get
# or
flutter pub get

Example usage:

import 'package:toon_x_json/toon_x_json.dart';

void main() {
  final data = {
    'users': [
      {'id': 1, 'name': 'Alice', 'role': 'admin'},
      {'id': 2, 'name': 'Bob', 'role': 'user'}
    ]
  };

  final toon = encode(data);
  print(toon);
  // users[2]{id,name,role}:
  //   1,Alice,admin
  //   2,Bob,user
}

Format Overview #

Note

For precise formatting rules and implementation details, see the full specification.

Objects #

Simple objects with primitive values:

encode({
  'id': 123,
  'name': 'Ada',
  'active': true
})
id: 123
name: Ada
active: true

Nested objects:

encode({
  'user': {
    'id': 123,
    'name': 'Ada'
  }
})
user:
  id: 123
  name: Ada

Arrays #

Tip

TOON includes the array length in brackets (e.g., items[3]). When using comma delimiters (default), the delimiter is implicit. When using tab or pipe delimiters, the delimiter is explicitly shown in the header (e.g., tags[2|] or [2 ]). This encoding helps LLMs identify the delimiter and track the number of elements, reducing errors when generating or validating structured output.

Primitive Arrays (Inline)

encode({
  'tags': ['admin', 'ops', 'dev']
})
tags[3]: admin,ops,dev

Arrays of Objects (Tabular)

When all objects share the same primitive fields, TOON uses an efficient tabular format:

encode({
  'items': [
    {'sku': 'A1', 'qty': 2, 'price': 9.99},
    {'sku': 'B2', 'qty': 1, 'price': 14.5}
  ]
})
items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5

Tabular formatting applies recursively: nested arrays of objects (whether as object properties or inside list items) also use tabular format if they meet the same requirements.

Note

Tabular format requires identical field sets across all objects (same keys, order doesn't matter) and primitive values only (strings, numbers, booleans, null).

Mixed and Non-Uniform Arrays

Arrays that don't meet the tabular requirements use list format:

items[3]:
  - 1
  - a: 1
  - text

When objects appear in list format, the first field is placed on the hyphen line:

items[2]:
  - id: 1
    name: First
  - id: 2
    name: Second
    extra: true

Arrays of Arrays

When you have arrays containing primitive inner arrays:

encode({
  'pairs': [
    [1, 2],
    [3, 4]
  ]
})
pairs[2]:
  - [2]: 1,2
  - [2]: 3,4

Empty Arrays and Objects

Empty containers have special representations:

encode({'items': []}) // items[0]:
encode([]) // [0]:
encode({}) // (empty output)
encode({'config': {}}) // config:

Quoting Rules #

TOON quotes strings only when necessary to maximize token efficiency:

  • Inner spaces are allowed; leading or trailing spaces force quotes.
  • Unicode and emoji are safe unquoted.
  • Quotes and control characters are escaped with backslash.

Note

When using alternative delimiters (tab or pipe), the quoting rules adapt automatically. Strings containing the active delimiter will be quoted, while other delimiters remain safe.

Object Keys and Field Names

Keys are unquoted if they match the identifier pattern: start with a letter or underscore, followed by letters, digits, underscores, or dots (e.g., id, userName, user_name, user.name, _private). All other keys must be quoted (e.g., "user name", "order-id", "123", "order:id", "").

String Values

String values are quoted when any of the following is true:

Condition Examples
Empty string ""
Leading or trailing spaces " padded ", " "
Contains active delimiter, colon, quote, backslash, or control chars "a,b" (comma), "a\tb" (tab), "a|b" (pipe), "a:b", "say \"hi\"", "C:\\Users", "line1\\nline2"
Looks like boolean/number/null "true", "false", "null", "42", "-3.14", "1e-6", "05"
Starts with "- " (list-like) "- item"
Looks like structural token "[5]", "{key}", "[3]: x,y"

Examples of unquoted strings: Unicode and emoji are safe (hello πŸ‘‹ world), as are strings with inner spaces (hello world).

Important

Delimiter-aware quoting: Unquoted strings never contain : or the active delimiter. This makes TOON reliably parseable with simple heuristics: split key/value on first : , and split array values on the delimiter declared in the array header. When using tab or pipe delimiters, commas don't need quoting – only the active delimiter triggers quoting for both array values and object values.

Type Conversions #

Some non-JSON types are automatically normalized for LLM-safe output:

Input Output
Number (finite) Decimal form, no scientific notation (e.g., -0 β†’ 0, 1e6 β†’ 1000000)
Number (NaN, Β±Infinity) null
BigInt If within safe integer range: converted to number. Otherwise: quoted decimal string (e.g., "9007199254740993")
DateTime ISO string in quotes (e.g., "2025-01-01T00:00:00.000Z")
Map Converted to object
Set Converted to array
List Converted to array

API #

encode(value: Object?, {EncodeOptions? options}): String #

Converts any JSON-serializable value to TOON format.

Parameters:

  • value – Any JSON-serializable value (Map, List, primitive, or nested structure). Non-JSON-serializable values are converted to null. Dates are converted to ISO strings, and BigInts are emitted as decimal integers (no quotes).
  • options – Optional encoding options:
    • indent – Number of spaces per indentation level (default: 2)
    • delimiter – Delimiter for array values and tabular rows: ',' (comma), '\t' (tab), '|' (pipe) (default: ',')
    • lengthMarker – Optional marker to prefix array lengths: '#' or null (default: null)
    • enforceFlatMap – If true, converts nested objects into a flat map by concatenating keys with a separator (default: false)
    • flatMapSeparator – Separator used when flattening nested objects (default: '_')

Returns:

A TOON-formatted string with no trailing newline or spaces.

Example:

import 'package:toon_x_json/toon_x_json.dart';

final items = [
  {'sku': 'A1', 'qty': 2, 'price': 9.99},
  {'sku': 'B2', 'qty': 1, 'price': 14.5}
];

encode({'items': items})

Output:

items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5

Delimiter Options

The delimiter option allows you to choose between comma (default), tab, or pipe delimiters for array values and tabular rows. Alternative delimiters can provide additional token savings in specific contexts.

Tab Delimiter (\t)

Using tab delimiters instead of commas can reduce token count further, especially for tabular data:

final data = {
  'items': [
    {'sku': 'A1', 'name': 'Widget', 'qty': 2, 'price': 9.99},
    {'sku': 'B2', 'name': 'Gadget', 'qty': 1, 'price': 14.5}
  ]
};

encode(data, options: EncodeOptions(delimiter: '\t'));

Output:

items[2	]{sku	name	qty	price}:
  A1	Widget	2	9.99
  B2	Gadget	1	14.5

Benefits:

  • Tabs are single characters and often tokenize more efficiently than commas.
  • Tabs rarely appear in natural text, reducing the need for quote-escaping.
  • The delimiter is explicitly encoded in the array header, making it self-descriptive.

Considerations:

  • Some terminals and editors may collapse or expand tabs visually.
  • String values containing tabs will still require quoting.
Pipe Delimiter (|)

Pipe delimiters offer a middle ground between commas and tabs:

encode(data, options: EncodeOptions(delimiter: '|'));

Output:

items[2|]{sku|name|qty|price}:
  A1|Widget|2|9.99
  B2|Gadget|1|14.5

Length Marker Option

The lengthMarker option adds an optional hash (#) prefix to array lengths to emphasize that the bracketed value represents a count, not an index:

final data = {
  'tags': ['reading', 'gaming', 'coding'],
  'items': [
    {'sku': 'A1', 'qty': 2, 'price': 9.99},
    {'sku': 'B2', 'qty': 1, 'price': 14.5},
  ],
};

encode(data, options: EncodeOptions(lengthMarker: '#'));
// tags[#3]: reading,gaming,coding
// items[#2]{sku,qty,price}:
//   A1,2,9.99
//   B2,1,14.5

// Custom delimiter with length marker
encode(data, options: EncodeOptions(
  lengthMarker: '#',
  delimiter: '|',
));
// tags[#3|]: reading|gaming|coding
// items[#2|]{sku|qty|price}:
//   A1|2|9.99
//   B2|1|14.5

Flat Map Option

The enforceFlatMap option allows you to convert nested JSON objects into a flat map structure, which can be useful for certain data processing scenarios or when working with systems that prefer flat key-value structures.

When enforceFlatMap is true, nested objects are flattened by concatenating keys with the specified flatMapSeparator. The decoder can then unflatten these keys back into nested objects.

Example:

final nested = {
  'a': {
    'b': 'x',
    'c': 42,
  }
};

// Encode with flat map
final toonFlat = encode(nested, options: EncodeOptions(
  enforceFlatMap: true,
  flatMapSeparator: '_',
));
// a_b: x
// a_c: 42

// Decode with unflattening
final decoded = decode(toonFlat, options: DecodeOptions(
  enforceFlatMap: true,
  flatMapSeparator: '_',
));
// {a: {b: 'x', c: 42}}

Use Cases:

  • Converting nested configuration objects to flat key-value pairs
  • Working with systems that require flat map structures
  • Simplifying nested data for certain processing pipelines

Custom Separator:

You can use any separator string (e.g., '.', '-', '::'):

final config = {
  'database': {
    'host': 'localhost',
    'port': 5432,
  }
};

encode(config, options: EncodeOptions(
  enforceFlatMap: true,
  flatMapSeparator: '.',
));
// database.host: localhost
// database.port: 5432

decode(input: String, {DecodeOptions? options}): Object? #

Converts a TOON-formatted string back to Dart values.

Parameters:

  • input – A TOON-formatted string to parse
  • options – Optional decoding options:
    • indent – Expected number of spaces per indentation level (default: 2)
    • strict – Enable strict validation (default: true)
    • enforceFlatMap – If true, unflattens flat map keys back into nested objects using the separator (default: false)
    • flatMapSeparator – Separator used when unflattening flat map keys (default: '_')

Returns:

A Dart value (Map, List, or primitive) representing the parsed TOON data.

Example:

import 'package:toon_x_json/toon_x_json.dart';

const toon = '''
items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5
''';

final data = decode(toon);
// {
//   'items': [
//     {'sku': 'A1', 'qty': 2, 'price': 9.99},
//     {'sku': 'B2', 'qty': 1, 'price': 14.5}
//   ]
// }

Strict Mode:

By default, the decoder validates input strictly:

  • Invalid escape sequences: Throws on "\x", unterminated strings.
  • Syntax errors: Throws on missing colons, malformed headers.
  • Array length mismatches: Throws when declared length doesn't match actual count.
  • Delimiter mismatches: Throws when row delimiters don't match header.

Notes and Limitations #

  • Format familiarity and structure matter as much as token count. TOON's tabular format requires arrays of objects with identical keys and primitive values only. When this doesn't hold (due to mixed types, non-uniform objects, or nested structures), TOON switches to list format where JSON can be more efficient at scale.
    • TOON excels at: Uniform arrays of objects (same fields, primitive values), especially large datasets with consistent structure.
    • JSON is better for: Non-uniform data, deeply nested structures, and objects with varying field sets.
    • CSV is more compact for: Flat, uniform tables without nesting. TOON adds structure ([N] length markers, delimiter scoping, deterministic quoting) that improves LLM reliability with minimal token overhead.
  • Token counts vary by tokenizer and model. Benchmarks use a GPT-style tokenizer (cl100k/o200k); actual savings will differ with other models (e.g., SentencePiece).
  • TOON is designed for LLM input where human readability and token efficiency matter. It's not a drop-in replacement for JSON in APIs or storage.

Using TOON in LLM Prompts #

TOON works best when you show the format instead of describing it. The structure is self-documenting – models parse it naturally once they see the pattern.

Sending TOON to LLMs (Input) #

Wrap your encoded data in a fenced code block (label it ```toon for clarity). The indentation and headers are usually enough – models treat it like familiar YAML or CSV. The explicit length markers ([N]) and field headers ({field1,field2}) help the model track structure, especially for large tables.

Generating TOON from LLMs (Output) #

For output, be more explicit. When you want the model to generate TOON:

  • Show the expected header (users[N]{id,name,role}:). The model fills rows instead of repeating keys, reducing generation errors.
  • State the rules: 2-space indent, no trailing spaces, [N] matches row count.

Here's a prompt that works for both reading and generating:

Data is in TOON format (2-space indent, arrays show length and fields).

```toon
users[3]{id,name,role,lastLogin}:
  1,Alice,admin,2025-01-15T10:30:00Z
  2,Bob,user,2025-01-14T15:22:00Z
  3,Charlie,user,2025-01-13T09:45:00Z
```

Task: Return only users with role "user" as TOON. Use the same header. Set [N] to match the row count. Output only the code block.

Tip

For large uniform tables, use encode(data, options: EncodeOptions(delimiter: '\t')) and tell the model "fields are tab-separated." Tabs often tokenize better than commas and reduce the need for quote-escaping.

Syntax Cheatsheet #

Show format examples
// Object
{ id: 1, name: 'Ada' }          β†’ id: 1
                                  name: Ada

// Nested object
{ user: { id: 1 } }             β†’ user:
                                    id: 1

// Primitive array (inline)
{ tags: ['foo', 'bar'] }        β†’ tags[2]: foo,bar

// Tabular array (uniform objects)
{ items: [                      β†’ items[2]{id,qty}:
  { id: 1, qty: 5 },                1,5
  { id: 2, qty: 3 }                 2,3
]}

// Mixed / non-uniform (list)
{ items: [1, { a: 1 }, 'x'] }   β†’ items[3]:
                                    - 1
                                    - a: 1
                                    - x

// Array of arrays
{ pairs: [[1, 2], [3, 4]] }     β†’ pairs[2]:
                                    - [2]: 1,2
                                    - [2]: 3,4

// Root array
['x', 'y']                      β†’ [2]: x,y

// Empty containers
{}                              β†’ (empty output)
{ items: [] }                   β†’ items[0]:

// Special quoting
{ note: 'hello, world' }        β†’ note: "hello, world"
{ items: ['true', true] }       β†’ items[2]: "true",true

Examples #

Check out the examples directory for comprehensive examples demonstrating:

  • Simple JSON encoding/decoding
  • Flat lists with different data types
  • Nested structures
  • Tabular arrays (best use case)
  • Mixed arrays
  • Custom encoding options
  • Flat map (flattening/unflattening nested objects)
  • Edge cases

Run examples with:

fvm dart run example/example.dart
fvm dart run example/1_simple_json.dart
# ... see example/README.md for all examples

Other Implementations #

Note

When implementing TOON in other languages, please follow the specification (currently v1.4) to ensure compatibility across implementations. The conformance tests provide language-agnostic test fixtures that validate implementations across any language.

Official Implementations #

Community Implementations #

Contributors #

License #

MIT License Β© 2025-PRESENT Johann Schopplich

4
likes
130
points
79
downloads

Publisher

unverified uploader

Weekly Downloads

TOON <--> JSON, TOON- A compact JSON format

Documentation

API reference

License

MIT (license)

More

Packages that depend on toon_x_json