Skip to content

feat: add rtk toon subcommand and filter for token-optimized JSON encoding#2897

Open
vishalneelkant wants to merge 1 commit into
rtk-ai:developfrom
vishalneelkant:feat/JSONToToon
Open

feat: add rtk toon subcommand and filter for token-optimized JSON encoding#2897
vishalneelkant wants to merge 1 commit into
rtk-ai:developfrom
vishalneelkant:feat/JSONToToon

Conversation

@vishalneelkant

@vishalneelkant vishalneelkant commented Jul 8, 2026

Copy link
Copy Markdown

Walkthrough — rtk toon Subcommand & Filter

We have implemented and verified the new rtk toon feature, which converts JSON documents to Token-Oriented Object Notation (TOON) v3.3 to optimize token usage when sending API responses or structured data to LLMs.

Changes Made

1. TOON Encoder Module

  • Created toon.rs containing a spec-compliant encoder:
    • Tabular conversion of uniform arrays of objects (significant token savings)
    • Indentation-based representation for objects (removes {} and : whitespace)
    • Inline primitive arrays (comma-separated with length prefix [N]:)
    • Expanded array representation (- prefix list items) for mixed/non-uniform array items
    • Safe, minimal quoting of keys and string values (quotes added only for colons, commas, braces, bracket syntax, numeric-like strings, or keywords)
    • Canonical number formatting (removing trailing fractional zeros and normalizing -0 to 0)
  • Registered toon in mod.rs

2. rtk toon Command & Integration

  • Created toon_cmd.rs to handle CLI invocation for files and stdin.
  • Updated Commands enum, CLI command router, and exit-code mapping in main.rs.
  • Added "toon" to RTK_META_COMMANDS in constants.rs to declare it as a internal tool command.

3. Piped Stream Filter

  • Added "toon" filter resolver in pipe_cmd.rs to support piped stream transformation, e.g. curl ... | rtk pipe --filter toon.

Validation Results

Automated Tests

Ran the full test suite and all 2420 tests passed successfully:

cargo fmt --all && cargo clippy --all-targets && cargo test --all

All unit tests in toon.rs passed, validating:

  • Tabular format formatting and headers.
  • Inline primitive arrays.
  • Mixed arrays and deep nesting.
  • Strings containing special characters (commas, colons) requiring quotes.
  • Redundant closure clippy rules.
  • Meta-commands inventory consistency tests.

Manual Verification

Tested the compiled debug binary against a sample JSON file, stdin input, and pipe filter:

$ ./target/debug/rtk toon test.json
users[2]{id,name}:
  1,Ada
  2,Linus

$ cat test.json | ./target/debug/rtk toon -
users[2]{id,name}:
  1,Ada
  2,Linus

$ cat test.json | ./target/debug/rtk pipe --filter toon
users[2]{id,name}:
  1,Ada
  2,Linus

All output styles perfectly match the expected TOON v3.3 spec format.

@vishalneelkant

Copy link
Copy Markdown
Author

rtk toon Token Savings Analysis

We evaluated the newly implemented rtk toon command against three typical JSON data structures. The metrics show substantial token and character savings, particularly for uniform arrays typical of REST API responses.

Key Findings

Data Structure JSON (Indented) Tokens JSON (Minified) Tokens TOON Tokens Savings vs Indented Savings vs Minified
Simple Object (Metadata/Config) 40 36 30 24.1% 16.7%
REST API Response (Uniform Array) 229 168 84 63.6% 50.3%
Deeply Nested Structure 140 71 61 56.8% 14.5%

Detailed Results

1. Simple Object (Metadata/Config)

  • Use Case: Config files, single database entry lookups, feature flags.
  • Conversion Benefit: Removes braces, spaces, quotes on keys/values, and trailing commas.
  • Analysis:
    • Raw JSON (158 chars):
      {
        "name": "Service Registry",
        "version": "1.4.2",
        "status": "healthy",
        "uptime_seconds": 86400,
        "debug_mode": false,
        "environment": "production"
      }
    • TOON (120 chars):
      name: Service Registry
      version: "1.4.2"
      status: healthy
      uptime_seconds: 86400
      debug_mode: false
      environment: production
      
    • Result: Saves 24.1% tokens over indented JSON, and 16.7% over minified JSON.

2. REST API Response (Uniform Array of Objects)

  • Use Case: Standard API endpoints returning search results, lists, or tables of records (e.g. users, products, log entries).
  • Conversion Benefit: This is where TOON shines. By declaring the schema once in the header (results[5]{id,first_name,last_name,email,role,active}:), we completely avoid repeating the object keys ("id", "first_name", etc.) for every single row.
  • Analysis:
    • Raw JSON (914 chars): Paginated response with 5 user objects.
    • TOON (333 chars):
      page: 1
      per_page: 5
      total: 150
      results[5]{id,first_name,last_name,email,role,active}:
        101,John,Doe,john.doe@example.com,admin,true
        102,Jane,Smith,jane.smith@example.com,user,true
        103,Bob,Johnson,bob.j@example.com,user,false
        104,Alice,Brown,alice.b@example.com,editor,true
        105,Charlie,Green,charlie.g@example.com,user,false
      
    • Result: Saves 63.6% tokens over indented JSON, and 50.3% tokens over minified JSON. This cuts token cost in half!

3. Deeply Nested Structure

  • Use Case: Multi-level hierarchical trees, complex domain models.
  • Conversion Benefit: Replaces heavy braces nesting with clean 2-space indentation.
  • Analysis:
    • Raw JSON (558 chars): Organization -> department -> teams.
    • TOON (241 chars):
      organization:
        name: Acme Corp
        departments[2]:
          - name: Engineering
            teams[2]{name,lead,size}:
              Core Platform,Alice,8
              Frontend UI,Bob,5
          - name: Marketing
            teams[1]{name,lead,size}:
              Growth,Charlie,3
      
    • Result: Saves 56.8% tokens over indented JSON, and 14.5% over minified JSON.

Conclusion

TOON is an exceptionally efficient representation format for feeding data into LLMs:

  1. REST API Responses: You can expect 50% to 65% token savings when converting standard paginated lists/results arrays.
  2. Deterministic & Less Ambiguity: In addition to saving tokens, the tabular format is highly readable and less prone to JSON bracket parsing errors by LLMs.
  3. Safety Guard (never_worse): Because rtk guards output using never_worse, in the rare case where TOON structure is longer than JSON (e.g. extremely small objects), it automatically falls back to raw JSON.

@vishalneelkant vishalneelkant changed the title changes for json to toon filter feat: add rtk toon subcommand and filter for token-optimized JSON encoding Jul 8, 2026
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants