Search by

mischasigtermans / laravel-toon

mischasigtermans

Laravel TOON: send data to LLMs and MCP clients in about half the tokens of JSON. Passes the TOON v4.1 spec test suite.

Package info

github.com/mischasigtermans/laravel-toon

pkg:composer/mischasigtermans/laravel-toon

Statistics

Installs: 94 924

Dependents: 0

Suggesters: 0

Stars: 149

Open Issues: 1

v2.0.1 2026-12-03 11:20 UTC

This package is auto-updated.

Last update: 2026-12-03 11:22:18 UTC


README

Latest Version on Packagist Total Downloads

Laravel TOON brings TOON to Laravel. TOON encodes JSON in about half the tokens, for data you send to an LLM. The package implements the TOON v4.1 spec, passes its official test suite, and is listed as the Laravel implementation.

Building with Laravel for Rust? laravel-toon-rust writes the same TOON.

Installation

Requires PHP 8.2 and Laravel 12 or 13.

composer require mischasigtermans/laravel-toon

Upgrading from 1.x? The 2.0.0 changelog says what changes and how to migrate.

Usage

use MischaSigtermans\Toon\Facades\Toon;

$data = [
    'users' => [
        ['id' => 1, 'name' => 'Alice', 'active' => true],
        ['id' => 2, 'name' => 'Bob', 'active' => false],
    ],
];

$toon = Toon::encode($data);
// users[2]{id,name,active}:
//   1,Alice,true
//   2,Bob,false

$data = Toon::decode($toon);

The toon_encode() and toon_decode() helpers do the same. Collections and Eloquent models have toToon():

$toon = collect($rows)->toToon();
$toon = User::find(1)->toToon();

diff measures the saving, and only encodes the keys you name:

Toon::diff($data);
// ['json_chars' => 12500, 'toon_chars' => 5200, 'saved_chars' => 7300, 'savings_percent' => 58.4]

Toon::only(User::all()->toArray(), ['id', 'name']);

Return TOON from an MCP tool, or from a route a client asks TOON of:

return response(Toon::encode($users->toArray()))
    ->header('Content-Type', 'text/toon; charset=utf-8');

What it saves

The field names of a table are written once, so the saving grows with the rows:

{"users":[{"id":1,"name":"Alice","role":"admin"},{"id":2,"name":"Bob","role":"user"},{"id":3,"name":"Carol","role":"user"}]}
users[3]{id,name,role}:
  1,Alice,admin
  2,Bob,user
  3,Carol,user

That's 124 bytes as JSON and 67 as TOON, 46% smaller. At 10 rows it's 56%, at 50 it's 59%. In a production application, 50 to 1000 records of real data came out 53% to 54% smaller.

The format

Arrays of objects with the same keys become a table. Nested objects that share a shape stay in the table as a field group:

Toon::encode([
    ['id' => 1, 'author' => ['name' => 'Jane', 'email' => 'jane@example.com']],
    ['id' => 2, 'author' => ['name' => 'John', 'email' => 'john@example.com']],
]);
// [2]{id,author{name,email}}:
//   1,Jane,jane@example.com
//   2,John,john@example.com

An object of objects with the same keys becomes a keyed table:

Toon::encode(['servers' => [
    'alpha' => ['host' => 'a.example.com', 'port' => 8080],
    'beta' => ['host' => 'b.example.com', 'port' => 9090],
]]);
// servers[2:]{host,port}:
//   alpha: a.example.com,8080
//   beta: b.example.com,9090

Arrays whose objects differ use list form, and arrays of scalars stay on one line. Strings with special characters are quoted, so Hello, World: How are you? reads back as written. Integers, floats, booleans and null keep their type through a round trip, and the decoder skips # comment lines.

Configuration

Publish the config file:

php artisan vendor:publish --tag=toon-config
Option Default What it does
min_rows_for_table 1 Arrays with fewer rows use list form. Above 1 the output no longer conforms to the spec.
indent 2 Spaces per indentation level.
delimiter ',' ',', "\t" or `'
strict true Decoding throws on length and width mismatches, malformed headers, duplicate keys and indentation errors.
omit [] Leave 'null' values, 'empty' strings, 'false', or 'all' three out of objects. Table cells keep them.
omit_keys [] Keys to leave out at every depth, such as created_at.
key_aliases [] Shorter names for keys, such as 'description' => 'desc'.
date_format null A PHP date format for dates and ISO 8601 strings, such as 'Y-m-d'.
truncate_strings null The most characters a string keeps, followed by ....
number_precision null The most decimal places a float keeps.

Notes

  • Objects decode to associative arrays. PHP casts integer-like keys, such as "42", to int, and an empty object decodes to [].
  • Integers beyond PHP_INT_MAX decode as float.
  • DateTimeInterface encodes as an ISO 8601 string, BackedEnum as its value, Arrayable, Traversable and JsonSerializable as their array form, and Stringable as its string.
  • In non-strict mode, each leading tab counts as one indentation level.

Testing

npm install
composer test

npm install fetches the official spec fixtures, which the suite runs next to its own tests.

License

MIT