toon-encode
Minimal TOON encoder for Rust — Token-Oriented Object Notation for LLM output.
TOON is a compact, human-readable encoding of JSON data that reduces token consumption by 30-50% for list-heavy responses. Field names are declared once in tabular headers instead of repeated on every row.
Usage
use Serialize;
let items = vec!;
// From any Serialize type
let toon = to_toon_string.unwrap;
// Output:
// [2]{name,value}:
// alpha,1
// beta,2
You can also encode a serde_json::Value directly:
let value = json!;
let toon = encode_toon;
// Output:
// status: ok
// items:
// [2]{file,kind,lines}:
// main.rs,fn,25
// lib.rs,mod,10
When TOON helps
TOON is most effective for flat tabular arrays — uniform objects with only primitive fields:
| Format | [{"name":"a","value":1},{"name":"b","value":2}] |
|---|---|
| JSON | 51 bytes |
| TOON | 28 bytes (-45%) |
For nested or content-heavy responses, TOON may be larger than JSON.
Encoding rules
- Objects: YAML-like
key: valuewith 2-space indentation - Uniform arrays of objects: Tabular
[N]{field1,field2,...}:header + CSV rows - Primitive arrays: List with
- valueper line - Strings: Quoted when containing
,,:,",\,[,],{,},\n,\r,\t, or starting with-
API
/// Encode any Serialize type as TOON.
;
/// Encode a serde_json::Value as TOON at the given indentation depth.
;
License
MIT