TOON Format - Token-Oriented Object Notation
A Rust implementation of the TOON (Token-Oriented Object Notation) format.
TOON is a compact, human-readable serialization format designed specifically to minimize tokens when sending structured data to Large Language Models (LLMs). It achieves 30-60% token reduction compared to JSON for uniform arrays while maintaining full lossless round-trip capability.
Features
- 🚀 Compact: YAML-like indentation + CSV-like tabular arrays
- 🔄 Lossless: JSON → TOON → JSON without data loss
- 📊 Schema-aware: Explicit array lengths
[N]and field lists{fields} - 🧠 LLM-optimized: Designed specifically for LLM prompts
- ⚡ Fast: Zero-copy parsing with minimal allocations
- 🔧 Flexible: Multiple delimiters (comma, tab, pipe)
Quick Start
use ;
// Parse TOON string
let input = r#"id: 123
name: Alice
active: true"#;
let value = parse.unwrap;
assert_eq!;
// Serialize to TOON
let output = to_string.unwrap;
assert!;
Examples
Simple Object
TOON:
id: 123
name: Alice
active: true
Equivalent JSON:
Nested Object
TOON:
user:
id: 1
name: Alice
Inline Array
TOON:
tags[3]: foo,bar,baz
Equivalent JSON:
Tabular Array (Major Token Savings!)
TOON:
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
Equivalent JSON:
Complex Document
TOON:
context:
task: Our favorite hikes together
location: Boulder
season: spring_2025
friends[3]: ana,luis,sam
hikes[3]{id,name,distanceKm,elevationGain,companion,wasSunny}:
1,Blue Lake Trail,7.5,320,ana,true
2,Ridge Overlook,9.2,540,luis,false
3,Wildflower Loop,5.1,180,sam,true
JSON Interoperability
use ;
// Convert JSON to TOON
let json = r#"{"id":123,"name":"Alice"}"#;
let toon = json_to_toon.unwrap;
// Convert TOON back to JSON
let json_back = toon_to_json.unwrap;
Installation
Add to your Cargo.toml:
[]
= "0.1"
Why TOON?
| Format | Tokens | Relative |
|---|---|---|
| JSON | 4,587 | 100% |
| YAML | 3,749 | 82% |
| JSON Compact | 3,104 | 68% |
| TOON | 2,759 | 60% |
Benchmark: 100 uniform employee records
Specification
This implementation follows the TOON Specification v3.0.
Key features:
- Indentation-based objects: No braces, YAML-like nesting
- Explicit array headers:
key[N]:for inline arrays,key[N]{fields}:for tabular arrays - Minimal quoting: Strings only quoted when necessary
- Multiple delimiters: Comma
,, tab\t, or pipe| - Canonical numbers: No scientific notation, no trailing zeros
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- TOON Format Organization for the specification
- Inspired by JSON, YAML, and CSV formats