TOON Format for Rust
Token-Oriented Object Notation (TOON) is a compact, human-readable format designed for passing structured data to Large Language Models with significantly reduced token usage.
This crate provides the official, spec-compliant Rust implementation of TOON, offering both a library (toon-format) and a full-featured command-line tool (toon).
Example
JSON (verbose):
TOON (compact):
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
Features
- Spec-Compliant: Fully compliant with TOON Specification v1.4.
- Safe & Performant: Built with safe, fast Rust.
- Serde Integration: Natively serializes from and deserializes to
serde_json::Valuefor easy integration. - Powerful CLI: A full-featured
toonbinary for command-line conversion. - Validation: Includes a strict mode decoder (on by default) to enforce all spec rules.
Library Usage
Add toon-format to your Rust project:
Basic encode and decode
The library works directly with serde_json::Value.
use json;
use ;
Options
You can customize the encoding and decoding process by passing EncodeOptions or DecodeOptions.
use ;
use json;
// --- Encode with options ---
let data = json!;
let encode_opts = new
.with_delimiter // Use '|' as delimiter
.with_indent // Use 4 spaces
.with_length_marker; // Add '#' to [N]
let toon_string = encode?;
// tags[#2|]: a|b
// --- Decode with options ---
let toon_input = "items[3]: a,b"; // Mismatched length
// Default (strict) decode will fail
assert!;
// Non-strict decode will pass
let decode_opts = new
.with_strict; // Disable length validation
let decoded = decode?;
assert_eq!;
CLI Usage
You can also install the toon binary for command-line use.
Installation
Basic Usage
The CLI auto-detects the operation based on the file extension.
.jsoninput will be encoded to TOON..tooninput will be decoded to JSON.
# Encode JSON to TOON (auto-detected)
# Decode TOON to JSON (auto-detected)
Piping
The CLI reads from stdin if no input file is provided, or if - is used as the input file.
# Pipe from stdin (defaults to ENCODE)
|
|
# Pipe from stdin and force DECODE
|
|
Stats
Use --stats during encoding to see token (cl100k_base) and byte-size savings.
|
}
| | | | |
+==============+======+======+=========+
| | | | |
| ) | | | |
Options
| Option | Short | Description |
|---|---|---|
[input] |
Input file path (reads from stdin if omitted or '-') | |
--output <file> |
-o |
Output file path (prints to stdout if omitted) |
--encode |
-e |
Force encode mode (JSON -> TOON) |
--decode |
-d |
Force decode mode (TOON -> JSON) |
--delimiter <val> |
Array delimiter: comma, tab, or pipe (encode only) |
|
--indent <num> |
-i |
Indentation size in spaces (default: 2) (encode only) |
--tabs |
Use tabs for indentation (encode only) | |
--length-marker |
Add # prefix to array lengths (e.g., items[#3]) (encode only) |
|
--stats |
Show byte size savings (encode only) | |
--no-strict |
Disable strict validation (decode only) | |
--no-coerce |
Disable type coercion (e.g., "true" -> true) (decode only) |
|
--json-indent <num> |
Indent output JSON with N spaces (decode only) |
CLI Examples
# Encode a file to stdout
# Encode a file to another file
# Encode with tab delimiters and 4-space indent
# Decode a file and pretty-print the JSON
# Decode from stdin
|
# Encode from stdin and save to file with stats
|
Resources
Contributing
Contributions are welcome! Please see the CONTRIBUTING.md file for guidelines.
License
MIT License © 2025-PRESENT Johann Schopplich and Shreyas K S