Skip to content
/ tooner Public

πŸš€ Token-efficient serialization for LLMs - Convert JSON/YAML/TOML to TOON format with 30-60% fewer tokens. Tree-shakable, TypeScript, ESM+CJS.

License

Notifications You must be signed in to change notification settings

dwekat/tooner

Repository files navigation

tooner

npm version License: MIT

Buy Me A Coffee

Token-efficient serialization for LLMs - Convert JSON/YAML/TOML to TOON format

Installation

npm install tooner

# Or with other package managers
pnpm add tooner
yarn add tooner

What is TOON?

Token-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.

Usage

Core API (Object ↔ TOON)

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

Format Converters (Tree-Shakable)

// 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);

CLI

# 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

Current Status

βœ… Implemented

  • βœ… 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)

πŸ“‹ TODO

  • ❌ Documentation:
    • Comprehensive API documentation
    • More usage examples
    • Performance benchmarks
    • Comparison with JSON/YAML/TOML

Development

# Install dependencies
pnpm install

# Run tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Build
pnpm build

# Lint
pnpm lint

# Format
pnpm format

Bundle Sizes (Estimated)

Tree-shakable design ensures you only bundle what you use:

  • tooner (core): ~4KB
  • tooner/json: ~4KB (no extra deps)
  • tooner/yaml: ~20KB (includes yaml parser)
  • tooner/toml: ~15KB (includes toml parser)

Architecture

Tree-Shaking First

  • Each entry point is completely independent
  • No shared state between converters
  • Core has zero dependencies
  • Format parsers only imported when needed

File Structure

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

Contributing

This project follows the official TOON specification. Contributions are welcome! Please see issues tagged with "good first issue" or "help wanted".

License

MIT Β© 2025

Resources

About

πŸš€ Token-efficient serialization for LLMs - Convert JSON/YAML/TOML to TOON format with 30-60% fewer tokens. Tree-shakable, TypeScript, ESM+CJS.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published