toon-encode 0.1.1

Minimal TOON encoder — Token-Oriented Object Notation for LLM output
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 21.05 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.39 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 56s Average build duration of successful builds.
  • all releases: 56s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SaschaOnTour/toon-encode
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SaschaOnTour

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 serde::Serialize;

#[derive(Serialize)]
struct Item { name: String, value: i32 }

let items = vec![
    Item { name: "alpha".into(), value: 1 },
    Item { name: "beta".into(), value: 2 },
];

// From any Serialize type
let toon = toon_encode::to_toon_string(&items).unwrap();
// Output:
// [2]{name,value}:
//   alpha,1
//   beta,2

You can also encode a serde_json::Value directly:

let value = serde_json::json!({
    "status": "ok",
    "items": [
        {"file": "main.rs", "kind": "fn", "lines": 25},
        {"file": "lib.rs", "kind": "mod", "lines": 10},
    ]
});

let toon = toon_encode::encode_toon(&value, 0);
// 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: value with 2-space indentation
  • Uniform arrays of objects: Tabular [N]{field1,field2,...}: header + CSV rows
  • Primitive arrays: List with - value per line
  • Strings: Quoted when containing ,, :, ", \, [, ], {, }, \n, \r, \t, or starting with -

API

/// Encode any Serialize type as TOON.
pub fn to_toon_string<T: serde::Serialize>(value: &T) -> Result<String, serde_json::Error>;

/// Encode a serde_json::Value as TOON at the given indentation depth.
pub fn encode_toon(value: &serde_json::Value, depth: usize) -> String;

License

MIT