terset 0.9.0

A compact text format for JSON records in LLM prompts. Same data, fewer input tokens.
Documentation

Highlights

  • Format 1.6 default, reading 1.0 through 1.6. Writes Format 1.6 by default (column constants, arithmetic and ISO-8601 sequences, string templates, defaults, nullable and sparse nested flattening, child tables, break rows, path entries) and decodes canonical text from every version (1.0 through 1.6).
  • Zero runtime dependencies. Default features use only the Rust standard library. Enable the optional serde feature for serde_json::Value and Serialize/Deserialize helpers.
  • Exact key order and binary64 parity. JsonObject preserves ECMAScript own-key order (array indices 0..=4294967294 first, then insertion order). JsonString handles valid UTF-8 with zero overhead and preserves lone UTF-16 surrogates losslessly.
  • 100% byte-for-byte parity. Passes every shared conformance suite (1.0 through 1.6 and write-profile) and matches @stackdeck/terset (JavaScript) and terset (Python) byte for byte across real datasets and seeded fuzz tests.
  • Write Profile v1 and v2. Includes write_prompt, read_rows, and streaming iter_rows for structured LLM output.

Install

Zero-dependency core:

[dependencies]
terset = "0.9"

With serde_json::Value and Serialize/Deserialize support:

[dependencies]
terset = { version = "0.9", features = ["serde"] }

Quick start

use terset::{decode, encode, rules_for, JsonValue};

fn main() -> Result<(), terset::Error> {
    let input = JsonValue::parse_json(
        r#"{"team":"core","users":[{"id":"u1","name":"Ada","role":"admin"},{"id":"u2","name":"Ben","role":"ops"}]}"#,
    )?;

    let text = encode(&input)?;
    let rules = rules_for(&text)?;
    let decoded = decode(&text)?;

    assert_eq!(decoded, input);
    println!("{rules}\n{text}");
    Ok(())
}

Pinning an earlier format version

use terset::{encode_with, EncodeOptions, FormatVersion, JsonValue};

let input = JsonValue::parse_json(r#"[{"id":"1","a":1},{"id":"2","a":1},{"id":"3","a":1}]"#)?;
let v15 = encode_with(&input, EncodeOptions::with_format(FormatVersion::V1_5))?;

Using the optional serde feature

#[cfg(feature = "serde")]
{
    use terset::{decode_serde, encode_serde, EncodeOptions};

    let input = serde_json::json!([
        {"id": "u1", "role": "admin", "score": 10},
        {"id": "u2", "role": "admin", "score": 20},
        {"id": "u3", "role": "admin", "score": 30}
    ]);
    let encoded = encode_serde(&input, EncodeOptions::default())?;
    let decoded = decode_serde(&encoded)?;
    assert_eq!(decoded, input);
}

Write profile (v1, v2, and streaming)

Use the write profile when your code defines the columns and a language model writes the rows.

use terset::{iter_rows, read_rows, write_prompt, WriteColumn, WriteColumnType, WriteDefault, WriteOptions};

let cols = [
    WriteColumn::new("id", WriteColumnType::String).with_prefix("ORD-"),
    WriteColumn::new("status", WriteColumnType::String)
        .with_default(WriteDefault::String("open".to_owned())),
    WriteColumn::new("active", WriteColumnType::Boolean),
];

// Compact v2 mode (trims trailing tabs, accepts 1/0 booleans, uses --- end line)
let opts = WriteOptions::compact();
let prompt = write_prompt(&cols, Some(&opts))?;

// Batch parse
let rows = read_rows("100\t\t1\n101\tclosed\t0\n---", &cols, Some(&opts))?;
assert_eq!(rows.len(), 2);

// Incremental streaming parse from LLM token chunks
let chunks = ["100\t\t", "1\n101\tclosed\t0\n", "---"];
let streamed: Vec<_> = iter_rows(chunks, &cols, Some(&opts))?
    .collect::<Result<Vec<_>, _>>()?;
assert_eq!(streamed, rows);

Throughput benchmark

Measured on Apple Silicon (cargo run --release --example bench, Format 1.6 encode and strict canonical decode):

Dataset JSON bytes Terset 1.6 bytes Rust enc (ms) JS enc (ms) Py enc (ms) Rust dec (ms) JS dec (ms) Py dec (ms) serde_json ser/de (ms) Rust vs JS enc Rust vs Py enc
11-case suite 118,257 53,911 26.5 53.4 112.9 33.3 38.3 143.8 0.46 / 4.45 2.0x 4.3x
NWS Weather (LWX) 75,164 23,576 5.9 9.1 123.3 7.5 11.1 212.1 0.10 / 0.32 1.5x 21.0x
USGS Earthquakes 157,389 53,480 6.5 9.3 127.9 12.6 18.2 171.6 0.35 / 4.69 1.4x 19.6x

Note: Terset decode runs full canonical validation (including re-encoding verification) on every call, whereas serde_json performs raw JSON parsing only.

Verification

Run the full conformance, write-profile, cross-language parity, and clippy checks:

cargo fmt -- --check
cargo clippy --all-targets --no-default-features -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings
cargo test --no-default-features
cargo test --all-features

License

MIT. See LICENSE.