TOON (Token-Oriented Object Notation) - Rust
A Rust implementation of Token-Oriented Object Notation (TOON), a compact, human-readable format designed for passing structured data to Large Language Models with significantly reduced token usage.
This is a Rust port of the original JavaScript/TypeScript implementation: @byjohann/toon.
Why TOON?
LLM tokens cost money, and standard JSON is verbose. TOON conveys the same information with 30-60% fewer tokens than JSON:
JSON (257 tokens):
TOON (166 tokens):
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= "1.0"
Quick Start
use encode;
use json;
Output:
user:
active: true
id: 123
name: Ada
tags[2]: reading,gaming
Features
- ๐ธ Token-efficient: 30โ60% fewer tokens than JSON
- ๐ฑ Minimal syntax: removes redundant punctuation
- ๐ Indentation-based structure: like YAML
- ๐งบ Tabular arrays: declare keys once, stream rows
- ๐ฆ Type-safe: built with Rust's strong type system
Examples
Objects
use encode;
use json;
let data = json!;
println!;
Output:
active: true
id: 123
name: Ada
Arrays of Objects (Tabular Format)
let data = json!;
println!;
Output:
items[2]{price,qty,sku}:
9.99,2,A1
14.5,1,B2
Custom Delimiters
Use tab or pipe delimiters for even more token savings:
use ;
let data = json!;
let mut options = default;
options.delimiter = Tab;
println!;
Output:
tags[3 ]: reading gaming coding
Length Markers
Add a # prefix to array lengths for clarity:
let mut options = default;
options.length_marker = Some;
let data = json!;
println!;
Output:
tags[#3]: a,b,c
API
encode(value: &serde_json::Value, options: Option<EncodeOptions>) -> String
Converts a serde_json::Value to TOON format.
Parameters:
value: A reference to aserde_json::Valueto encodeoptions: Optional encoding options (defaults to standard TOON format)
Returns: A String containing the TOON-formatted output
EncodeOptions
Delimiter
Format Overview
- Objects:
key: valuewith 2-space indentation for nesting - Primitive arrays: Inline with count, e.g.,
tags[3]: a,b,c - Arrays of objects: Tabular format with header, e.g.,
items[2]{id,name}: - Mixed arrays: List format with
-prefix - Quoting: Only when necessary (special chars, structural ambiguity)
License
MIT License ยฉ 2025
See Also
Original JS/TS implementation: @byjohann/toon