Laravel TOON
============

 ★ 148 *v2.0.0* 

The TOON format for Laravel. The same data as JSON, in far fewer tokens.

 ```
composer require mischasigtermans/laravel-toon
```

 [Install Laravel TOON](https://github.com/mischasigtermans/laravel-toon) [View on GitHub](https://github.com/mischasigtermans/laravel-toon) 

JSON is an expensive format to pay for per token. Every record repeats every key, and the model reads the braces and quotes as carefully as the data. [TOON](https://toonformat.dev/), Token-Oriented Object Notation, writes the field names once and the records as rows.

I built this one because [nested Stagent bookings were eating my MCP server's context](/thought/why-i-built-laravel-toon), and the TOON packages for Laravel at the time kept the nesting intact. That post has the numbers on real Stagent data, and [Stagent](https://stagent.com) uses it in production. The package follows the TOON spec and its official test suite, and toonformat.dev lists it as the Laravel implementation.

TOON beats JSON on uniform arrays: database rows, paginated API responses, MCP tool results. On deeply nested, non-uniform data the saving shrinks, and a single object can be as small in JSON. `Toon::diff()` tells you which case you're in.

It doesn't replace JSON in your API. Serve TOON to the clients that ask for it, like a model or a token-conscious consumer, and keep JSON for everyone else.

What it does
------------

- Tables instead of repeated keysAn array of uniform objects becomes a table: field names once in the header, one row per record. The keys are written once, so the saving over JSON grows with every row.
- Nested objects stay in the tableNested objects that share one shape become nested field groups, like {id,author{name,email}}, at any depth. Objects of uniform objects collapse into keyed tables. Arrays whose objects differ in shape fall back to list form.
- Built to the specImplements the TOON specification and runs every fixture of its official test suite. Decoding gives back the original structure, with ints, floats, booleans and nulls intact.
- Laravel-shapedToon::encode() and Toon::decode(), toon\_encode() and toon\_decode() helpers, a toToon macro on collections and a toToon method on Eloquent models.
- Knobs for fewer tokensOmit nulls, empty strings or false values, drop keys like timestamps, alias long key names, truncate strings and cap float precision. Toon::diff() measures the saving in characters before you commit.

[Install Laravel TOON](https://github.com/mischasigtermans/laravel-toon).
