toonX

Dart implementation of TOON (Token-Oriented Object Notation) - a compact, human-readable format for LLMs that uses ~40% fewer tokens than JSON while maintaining better accuracy.
๐ฏ What is TOON?
TOON combines YAML's indentation-based structure with CSV-style tabular arrays to create the most efficient format for passing structured data to Large Language Models. It's a lossless, drop-in replacement for JSON designed specifically for LLM inputs.
Think: JSON for machines, TOON for AI models.
๐ฆ What This Package Supports
Core Functionality:
- โ
encode()- Convert Dart objects to TOON format - โ
decode()- Parse TOON strings back to Dart objects - โ
yamlToToon()- Convert YAML to TOON format - โ
toonToYaml()- Convert TOON to YAML format - โ
xmlToToon()- Convert XML to TOON format (Parker & Badgerfish) - โ
toonToXml()- Convert TOON to XML format - โ Lossless round-trip conversion with full JSON data model support
Encoding Options:
- โ
Custom delimiters - comma (
,), tab (\t), or pipe (|) for arrays - โ
Length markers - optional
#prefix for array validation (e.g.,tags[#3]) - โ Custom indentation - configurable spaces per level (default: 2)
- โ Tabular arrays - automatic CSV-style format for uniform objects
- โ Flat map mode - flatten nested structures with custom separators
- โ Smart quoting - minimal quoting, only when necessary
Decoding Options:
- โ Strict mode - validates array lengths, delimiters, and syntax (default)
- โ Lenient mode - best-effort parsing for malformed input
- โ Unflatten support - reconstruct nested structures from flat maps
- โ Escape sequence handling - proper handling of special characters
Additional Features:
- โ CLI tool - command-line interface for JSON/YAML/XML/TOON conversion
- โ Comprehensive tests - 105+ tests including large datasets
- โ Full documentation - detailed API docs for all functions
- โ Type-safe - strongly typed Dart implementation
๐ก Why TOON?
Token Efficiency
TOON significantly reduces token usage compared to other formats:
| Format | Tokens | vs TOON |
|---|---|---|
| TOON | 2,744 | baseline |
| JSON (compact) | 3,081 | +12% |
| YAML | 3,719 | +36% |
| JSON | 4,545 | +66% |
| XML | 5,167 | +88% |
LLM Accuracy
Benchmark results across 4 models on 209 data retrieval questions:
- TOON: 73.9% accuracy
- JSON: 69.7% accuracy
- YAML: 69.0% accuracy
- XML: 67.1% accuracy
Result: TOON achieves higher accuracy while using fewer tokens.
๐ Quick Start
Installation
dependencies:
toonx: ^1.0.0
Basic Usage
import 'package:toonx/toonx.dart';
final data = {
'users': [
{'id': 1, 'name': 'Alice', 'role': 'admin'},
{'id': 2, 'name': 'Bob', 'role': 'user'},
]
};
// Encode to TOON
final toon = encode(data);
print(toon);
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// Decode back to Dart
final decoded = decode(toon);
print(decoded); // Original data restored
๐ก Why TOON?
Token Efficiency
TOON significantly reduces token usage compared to other formats:
| Format | Tokens | vs TOON |
|---|---|---|
| TOON | 2,744 | baseline |
| JSON (compact) | 3,081 | +12% |
| YAML | 3,719 | +36% |
| JSON | 4,545 | +66% |
| XML | 5,167 | +88% |
LLM Accuracy
Benchmark results across 4 models on 209 data retrieval questions:
- TOON: 73.9% accuracy
- JSON: 69.7% accuracy
- YAML: 69.0% accuracy
- XML: 67.1% accuracy
Result: TOON achieves higher accuracy while using fewer tokens.
๐จ Advanced Features
Custom Delimiters
Use tabs or pipes for maximum efficiency:
// Tab delimiter - best for token efficiency
encode(data, options: EncodeOptions(delimiter: '\t'));
// Pipe delimiter - human-readable
encode(data, options: EncodeOptions(delimiter: '|'));
Flat Map
Flatten deeply nested structures:
final config = {
'database': {'connection': {'host': 'localhost', 'port': 5432}}
};
// Flatten
final flat = encode(
config,
options: EncodeOptions(enforceFlatMap: true, flatMapSeparator: '.'),
);
// database.connection.host: localhost
// database.connection.port: 5432
// Unflatten on decode
final restored = decode(
flat,
options: DecodeOptions(enforceFlatMap: true, flatMapSeparator: '.'),
);
Strict vs Lenient Parsing
// Strict mode (default) - validates array lengths and structure
decode(toon, options: DecodeOptions(strict: true));
// Lenient mode - best-effort parsing
decode(toon, options: DecodeOptions(strict: false));
Length Markers
Explicit array size validation:
encode(data, options: EncodeOptions(lengthMarker: '#'));
// tags[#3]: admin,ops,dev
YAML Support
Convert between YAML and TOON formats:
// YAML โ TOON
final yamlString = '''
users:
- id: 1
name: Alice
role: admin
- id: 2
name: Bob
role: user
''';
final toon = yamlToToon(yamlString);
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// TOON โ YAML
final yaml = toonToYaml(toon);
print(yaml);
XML Support
Convert between XML and TOON formats using the xml2json package:
// XML โ TOON (Parker convention - lightweight, no attributes)
final xmlString = '''
<users>
<user>
<id>1</id>
<name>Alice</name>
<role>admin</role>
</user>
<user>
<id>2</id>
<name>Bob</name>
<role>user</role>
</user>
</users>
''';
final toon = xmlToToon(xmlString);
// users:
// user[2]{id,name,role}:
// "1",Alice,admin
// "2",Bob,user
// TOON โ XML
final xml = toonToXml(toon);
print(xml);
// For XML with attributes/namespaces, use Badgerfish convention
final toonBadgerfish = xmlToToonBadgerfish(xmlString);
XML Conventions Supported:
- Parker (default) - Lightweight, ideal for LLM use cases
- Badgerfish - Preserves attributes and namespaces
XML support is powered by the excellent xml2json package by darticulate.com.
๐ง CLI Tool
Convert JSON, YAML, XML, or TOON files via command line:
# JSON โ TOON
dart run toonx input.json
# YAML โ TOON
dart run toonx config.yaml
# XML โ TOON
dart run toonx data.xml
# TOON โ JSON (default)
dart run toonx data.toon
# Encode with explicit command
dart run toonx encode input.json
# Decode with explicit command
dart run toonx decode data.toon
# Pipe from stdin
cat data.json | dart run toonx encode
echo '{"name": "Alice"}' | dart run toonx encode
# Show help
dart run toonx --help
Supported file formats:
.jsonโ Auto-converts to TOON.yaml,.ymlโ Auto-converts to TOON.xmlโ Auto-converts to TOON.toonโ Auto-converts to JSON
๐ When to Use TOON
Perfect for:
- โ Uniform arrays of objects (e-commerce orders, user records, logs)
- โ LLM prompts with structured data
- โ API responses for AI models
- โ Time-series data and analytics
- โ Configuration files for AI systems
Consider alternatives:
- โ Deeply nested, non-uniform structures (JSON may be better)
- โ Pure tabular data (CSV is smaller but less structured)
- โ Binary data or extremely large payloads
๐ Examples
See comprehensive examples in example/example.dart:
- Basic encoding and decoding
- Tabular arrays for uniform data
- Custom delimiters (tab, pipe)
- Length markers
- Flat map for nested structures
- Strict vs lenient parsing
- Real-world e-commerce example
๐งช Benchmarks
This package includes extensive tests with real-world data:
- 100+ employee records
- E-commerce orders with nested structures
- GitHub repositories data
- Analytics time-series
- Configuration files
Run tests: dart test
๐ Credits & Reference
This Dart implementation is based on the TOON (Token-Oriented Object Notation) format, an innovative data format designed for efficient LLM communication. Special thanks to the TOON format creators for their excellent work on this specification.
Special Thanks:
- xml2json by darticulate.com - Powers our XML conversion capabilities with support for Parker, Badgerfish, GData, and OpenRally conventions. A battle-tested package with 126 likes and 155 pub points.
๐ค Contributing
Contributions are welcome! To contribute to this package:
- Fork this repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes and add tests
- Ensure all tests pass (
dart test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to your branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please report bugs and feature requests in our issue tracker.
Made with โค๏ธ for the Dart & Flutter community