simple_toon

TOON (Token-Oriented Object Notation) Encoder/Decoder for Eiffel

v1.0.0 MIT 30-60% Token Reduction

Overview

TOON is a compact, human-readable format designed to minimize token usage when sending structured data to Large Language Models. simple_toon provides seamless conversion between JSON and TOON formats, achieving 30-60% token reduction while maintaining lossless round-trip conversion.

Part of the Simple Eiffel ecosystem.

Format Comparison

Simple Objects

JSON (37 tokens)

{
  "name": "Alice",
  "age": 30,
  "active": true
}

TOON (15 tokens)

name: Alice
age: 30
active: true

Tabular Arrays

JSON Array (85 tokens)

[
  {"sku": "A1", "name": "Widget", "price": 9.99},
  {"sku": "B2", "name": "Gadget", "price": 14.50}
]

TOON Tabular (28 tokens)

[2]{sku,name,price}:
  A1,Widget,9.99
  B2,Gadget,14.5

Quick Start

Installation

  1. Set the environment variable:
    export SIMPLE_EIFFEL=/d/prod
  2. Add to your ECF file:
    <library name="simple_toon" location="$SIMPLE_EIFFEL/simple_toon/simple_toon.ecf"/>

Encode & Decode

local
    toon: SIMPLE_TOON
    json: SIMPLE_JSON
    value: SIMPLE_JSON_VALUE
    toon_text, json_text: STRING_32
do
    create toon.make
    create json

    -- Encode JSON to TOON
    value := json.parse ("{%"name%": %"Alice%", %"age%": 30}")
    toon_text := toon.encode (value)
    -- Result: "name: Alice%Nage: 30"

    -- Decode TOON to JSON
    value := toon.decode ("name: Bob%Nage: 25")
    json_text := value.to_json_string
    -- Result: {"name":"Bob","age":25}

    -- Check compression ratio
    print ("Compression: " + (toon.compression_ratio (value) * 100).out + "%%")
end

Decimal Precision

simple_toon integrates with simple_decimal and simple_json's decimal support to ensure exact number representation without floating-point artifacts.

Problem with REAL

-- Using put_real
obj.put_real (19.99, "price")
-- JSON output: {"price": 19.989999999999998}
-- TOON output: price: 19.989999999999998

Solution with SIMPLE_DECIMAL

-- Using put_decimal
create price.make ("19.99")
obj.put_decimal (price, "price")
-- JSON output: {"price": 19.99}
-- TOON output: price: 19.99

For financial data or any values requiring exact decimal representation, use put_decimal with SIMPLE_DECIMAL instead of put_real.

Key Features

30-60% Token Reduction

Eliminates JSON's verbose syntax (braces, quotes, repeated keys).

Tabular Arrays

Uniform object arrays encoded as compact tables with headers.

Lossless Round-trip

Convert JSON to TOON and back without data loss.

Configurable

Adjustable indentation and delimiters (comma, tab, pipe).

Strict Validation

Optional strict mode with detailed error reporting.

Decimal Precision

Integrates with simple_decimal for exact number handling.

API Reference

SIMPLE_TOON

Main facade class for encoding/decoding TOON format.

Encoding (JSON to TOON)

encode (json: SIMPLE_JSON_VALUE): STRING_32
json_to_toon (json_text: STRING_32): STRING_32
encode_file (json_path, toon_path: STRING_32)

Decoding (TOON to JSON)

decode (toon_text: STRING_32): SIMPLE_JSON_VALUE
toon_to_json (toon_text: STRING_32): STRING_32
decode_file (toon_path, json_path: STRING_32)

Configuration

set_indent (spaces: INTEGER)
set_delimiter (char: CHARACTER_32)
set_strict_mode (enabled: BOOLEAN)

Analysis

compression_ratio (json: SIMPLE_JSON_VALUE): REAL_64
is_tabular_eligible (json: SIMPLE_JSON_VALUE): BOOLEAN
token_estimate (value): TUPLE [json_tokens, toon_tokens: INTEGER]

Configuration Options

Option Method Values Default
Indentation set_indent Any positive integer 2 spaces
Delimiter set_delimiter ',', '%T' (tab), '|' Comma
Strict Mode set_strict_mode True / False True

TOON Syntax Reference

Objects

key: value
nested:
  inner_key: inner_value

Primitive Arrays

[3]: red,green,blue

Tabular Arrays

[2]{sku,qty,price}:
  A1,10,9.99
  B2,20,14.50

List Arrays

[2]:
  - id: 1
    name: Alice
  - id: 2
    name: Bob

When to Use TOON

Best For

  • Uniform arrays of objects (product lists, records)
  • Flat or shallow nested structures
  • LLM prompt context optimization
  • Reducing API costs

Consider JSON For

  • Deeply nested structures
  • Non-uniform data
  • LLM output (models trained on JSON)
  • Inter-service communication

Resources