toonx 1.0.0
toonx: ^1.0.0 copied to clipboard
Dart implementation of TOON (Token-Oriented Object Notation) - a compact format for LLMs with 30-60% fewer tokens than JSON.
toonX #
[TOON Format Diagram]
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 - β 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 file/stream conversion
- β Comprehensive tests - 46+ 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
π§ CLI Tool #
Encode and decode via command line:
# Encode JSON to TOON
dart run toonx encode input.json -o output.toon
# Decode TOON to JSON
dart run toonx decode data.toon -o output.json
# Pipe from stdin
cat data.json | dart run toonx encode
# Show help
dart run toonx --help
π 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
π Specification #
This implementation follows the TOON Specification v2.0 for cross-language compatibility.
π€ Contributing #
Contributions welcome! Please check the official TOON repository for the full specification and conformance tests.
π License #
MIT License - see LICENSE file for details.
π Links #
- GitHub Repository
- pub.flutter-io.cn Package
- API Documentation
- Issue Tracker
- TOON Official Specification
- Benchmarks & Accuracy Tests
- Interactive Playground
Made with β€οΈ for the Dart & Flutter community