DEV Community

Cover image for TOON, the new JSON Alternative
Thomas Hansen
Thomas Hansen

Posted on

TOON, the new JSON Alternative

Everybody is talking about TOON today, and how it could reduce token consumption in AI agents. On average, TOON saves you somewhere between 30 and 50 percent, with the tradeoff being it's much less humanly readable. In some ways it's the "machine code equivalent" of JSON.

The whole point with TOON is to preserve LLM tokens in AI agents. Below is an example.

users[2]{id,name,role}:
  1,Alice,admin
  2,Bob,user
Enter fullscreen mode Exit fullscreen mode

The above is the equivalent to the following JSON.

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "role": "admin"
    },
    {
      "id": 2,
      "name": "Bob",
      "role": "user"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

If you don't understand the syntax, then realise it first mentions the field name of objects and collections, for then to just provide a "CSV lookalike" list of values. It took me about 5 seconds to understand the structure, and it took me about 5 minutes to dismiss it as crazy!

Ignoring the fact that the above JSON is formatted, most people can immediately understand how TOON reduces tokens. First of all, it only mentions the field name once, instead of once for every object in JSON.

Why it even cares to mention "row count" is a mystery to me for the record. It sticks out in my eyes like a soar thumb. Why everybody sharing this crap doesn't immediately understand this is still a mystery to me. The result is you loose your "single source of truth". Why not simple infer the row count from the number of rows? WTF happened to "single source of truth"? And WTF do we do with null values versus undefined? And yes, there is a difference!

However, its biggest problem is added cognitive complexity, and less readability.

TOON, is it worth it?

The problem with TOON is that it's simply not enough savings to justify the additional complexity, and the lesser readability. Even 50% savings on tokens is not really considered enough to justify an optimisation with semantic changes. If we were talking about 80 to 90 percent savings, it would be possible to intelligently discuss the thing. However, at most you save 50%!

If you look at a programming language like Hyperlambda for instance, you will see a reduction of 95% of your codebase compared to C#, Java, Python and GoLang. TOON has a reduction of 30 to 50 percent. And with Hyperlambda, apparently 95% token reduction isn't enough apparently, consider its relative adoption compared to the alternatives.

However, I have zero doubts that more people are literally using TOON today than Hyperlambda, which should tell you something about just how frikkin' ridiculously stupid "Homo sapiens" actually is ...

In addition, LLMs don't understand TOON the way they understand JSON. This significantly increases the amount of bugs during code generation.

If you don't accept the ideas of Hyperlambda, which is a 95% reduction, why would you choose to accept the ideas of TOON that only gives you 30 to 50 percent?

Hyperlambda

Hyperlambda is a declarative programming language. Below is a piece of Hyperlambda code to give you an idea of how it looks like.

.arguments
data.connect:crm
   data.read
      table:contacts
   return-nodes:x:-
Enter fullscreen mode Exit fullscreen mode

If I was to create the equivalent in Python, I'd end up with the following code.

import sqlite3
from fastapi import FastAPI
from typing import List, Dict, Any

app = FastAPI(title="CRM API")


class CRMDatabase:
    def __init__(self, db_path: str = "crm.db"):
        self.db_path = db_path

    def read_contacts(self) -> List[Dict[str, Any]]:
        conn = sqlite3.connect(self.db_path)
        conn.row_factory = sqlite3.Row
        cur = conn.cursor()

        cur.execute("SELECT * FROM contacts;")
        rows = cur.fetchall()

        conn.close()
        return [dict(row) for row in rows]


crm = CRMDatabase()

@app.get("/contacts")
def get_contacts():
    return {"contacts": crm.read_contacts()}
Enter fullscreen mode Exit fullscreen mode

I still find it fascinating that most dev heads claims the Python version is more readable. Personally, I believe considering Python more readable than Hyperlambda is a sign of permanent brain damage. Considering the Hyperlambda version is also using an "ORM" (kind of), the actual equivalent we'd look at becomes:

from fastapi import FastAPI
from typing import List
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base, Session
from sqlalchemy import Column, Integer, String

DATABASE_URL = "sqlite:///crm.db"  # Equivalent to data.connect:crm

engine = create_engine(
    DATABASE_URL,
    connect_args={"check_same_thread": False},  # Needed for SQLite + FastAPI
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

class Contact(Base):
    __tablename__ = "contacts"  # Equivalent to table:contacts

    id = Column(Integer, primary_key=True, index=True)
    first_name = Column(String)
    last_name = Column(String)
    email = Column(String)

app = FastAPI(title="CRM API")

def get_db():
    db: Session = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app.get("/contacts")
def get_contacts(db: Session = next(get_db())):
    contacts: List[Contact] = db.query(Contact).all()
    return {"contacts": [serialize_contact(c) for c in contacts]}

def serialize_contact(c: Contact) -> dict:
    return {
        "id": c.id,
        "first_name": c.first_name,
        "last_name": c.last_name,
        "email": c.email,
    }
Enter fullscreen mode Exit fullscreen mode

So if you're one of those advocating TOON today, let me ask you a questions; Why don't you advocate Hyperlambda instead?

I already know the answer to that of course, which is that you're an "imbecile parrot", incapable of sharing something unless 95% of your friends have already told you it's OK to share.

However, that creates problems for the world, because actually smart people like me, contrary to the idiot inventing TOON, now has to spend time talking about something that's about as interesting as a yawn into the wind - Because monkeys like you believes TOON is a great idea, because "somebody told you it was" - And it was probably an influencer that was literally paid to share it ...

However, TOON is a ridiculous idea, and I doubt you can even describe everything in it that you can describe in JSON (null vs undefined e.g.). And the only reason we're even talking about it, is because you're too stupid to understand the basics of software development theory.

So my suggestion to you is; Go through your social media accounts, check if you shared something about TOON. If you did, please edit your BIO on that social media platform to the following ...

Has no fucking idea about anything related to computing! But I like turtles!

Because that's literally how fucking stupid an idea this TOON rubbish actually was ...

Top comments (0)