Token-efficient serialization for LLMs - Convert JSON/YAML/TOML to TOON format
npm install tooner
# Or with other package managers
pnpm add tooner
yarn add toonerToken-Oriented Object Notation (TOON) is a compact, human-readable serialization format designed for passing structured data to Large Language Models with significantly reduced token usage (typically 30-60% fewer tokens than JSON).
TOON's sweet spot is uniform arrays of objects β multiple fields per row, same structure across items. See the official specification for complete details.
import { encode, decode } from 'tooner';
const data = {
users: [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' },
],
};
// Encode to TOON
const toon = encode(data);
console.log(toon);
// Output:
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// Decode from TOON
const decoded = decode(toon);
// Returns original data structure// JSON β TOON
import { encode, decode } from 'tooner/json';
const jsonString = '{"name":"Alice","age":30}';
const toon = encode(jsonString);
// YAML β TOON
import { encode as yamlEncode } from 'tooner/yaml';
const yamlString = 'name: Alice\nage: 30';
const toon = yamlEncode(yamlString);
// TOML β TOON
import { encode as tomlEncode } from 'tooner/toml';
const tomlString = 'name = "Alice"\nage = 30';
const toon = tomlEncode(tomlString);# Encode JSON to TOON
npx tooner encode input.json -o output.toon
# Encode YAML to TOON
npx tooner encode input.yaml -f yaml -o output.toon
# Decode TOON to JSON
npx tooner decode input.toon -o output.json
# Decode TOON to YAML
npx tooner decode input.toon -f yaml -o output.yaml- β Project structure with tree-shakable exports
- β TypeScript configuration with strict mode
- β Build system (tsup) with dual package support (ESM + CJS)
- β CLI tool with commander
- β Format converter structure (JSON, YAML, TOML)
- β
Complete TOON Encoder:
- Primitive values (strings, numbers, booleans, null)
- Objects and nested objects
- Inline arrays:
tags[3]: a,b,c - List format with hyphens for mixed arrays
- Tabular format for uniform object arrays
- Root-level arrays (all formats)
- Alternative delimiters (comma, tab, pipe)
- Proper key quoting and escaping
- Whitespace handling
- β
Complete TOON Decoder (363/363 tests passing - 100%):
- Parse TOON indentation structure
- Parse inline arrays with all delimiters
- Parse list format with nested objects
- Parse tabular format
- Handle all primitive types (including scientific notation)
- Path expansion with
expandPaths: 'safe'option - Strict mode with indentation validation
- Custom indent sizes
- Validate array lengths and field counts
- Error handling with line numbers
- Escape sequence handling
- β Test infrastructure with Vitest
- β Official TOON test fixtures (363/363 passing - 100%)
- β Security hardened (ReDoS vulnerabilities patched)
- β Documentation:
- Comprehensive API documentation
- More usage examples
- Performance benchmarks
- Comparison with JSON/YAML/TOML
# Install dependencies
pnpm install
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Build
pnpm build
# Lint
pnpm lint
# Format
pnpm formatTree-shakable design ensures you only bundle what you use:
tooner(core): ~4KBtooner/json: ~4KB (no extra deps)tooner/yaml: ~20KB (includes yaml parser)tooner/toml: ~15KB (includes toml parser)
- Each entry point is completely independent
- No shared state between converters
- Core has zero dependencies
- Format parsers only imported when needed
tooner/
βββ src/
β βββ core/
β β βββ encoder.ts # TOON encoder
β β βββ decoder.ts # TOON decoder (TODO)
β β βββ types.ts # Shared types
β βββ json.ts # Entry: tooner/json
β βββ yaml.ts # Entry: tooner/yaml
β βββ toml.ts # Entry: tooner/toml
β βββ index.ts # Entry: tooner
βββ cli/
β βββ index.ts # CLI tool
βββ tests/
βββ fixtures/ # Official TOON test fixtures
βββ unit/ # Unit tests
βββ integration/ # Integration tests
βββ performance/ # Benchmarks
This project follows the official TOON specification. Contributions are welcome! Please see issues tagged with "good first issue" or "help wanted".
MIT Β© 2025
