PHPackages                             sbsaga/toon - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sbsaga/toon

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sbsaga/toon
===========

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

v1.3.0(4mo ago)6665.2k↓15.6%9MITPHPPHP &gt;=8.1CI passing

Since Nov 2Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/sbsaga/toon)[ Packagist](https://packagist.org/packages/sbsaga/toon)[ Docs](https://github.com/sbsaga/toon)[ RSS](/packages/sbsaga-toon/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (10)Dependencies (9)Versions (23)Used By (0)

TOON for Laravel
================

[](#toon-for-laravel)

### Compact, human-readable, and token-efficient structured data for AI prompts, logs, and Laravel workflows

[](#compact-human-readable-and-token-efficient-structured-data-for-ai-prompts-logs-and-laravel-workflows)

 [![TOON logo](https://raw.githubusercontent.com/sbsaga/toon/main/assets/logo.webp)](https://raw.githubusercontent.com/sbsaga/toon/main/assets/logo.webp)

 [![Latest version](https://camo.githubusercontent.com/719e416482be5edbfcb40a917924fdcef3a80fd9ae4dad32791cb432f9f46931/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7362736167612f746f6f6e)](https://packagist.org/packages/sbsaga/toon) [![Total downloads](https://camo.githubusercontent.com/ca1bc5c4283b854305fd97276b2eec493f456d2e54258f8bc4627ee48e9e80ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7362736167612f746f6f6e)](https://packagist.org/packages/sbsaga/toon) [![Tests](https://camo.githubusercontent.com/5650f385e3d38e417d4ebc27f2d8d4168dd84f178b455b091e5704e7d195b97f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7362736167612f746f6f6e2f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://github.com/sbsaga/toon/actions/workflows/tests.yml) [![License](https://camo.githubusercontent.com/ffdfd38e79314062adc9d39e2385cf76f61045d8d9973f8771c0637630bf906c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7362736167612f746f6f6e)](https://github.com/sbsaga/toon/blob/main/LICENSE) [![Laravel 9+](https://camo.githubusercontent.com/89245a70bda21d9d050eec758336e80f71cc6fe19782af8eb261d7eb3d14553b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d392532422d6f72616e6765)](https://camo.githubusercontent.com/89245a70bda21d9d050eec758336e80f71cc6fe19782af8eb261d7eb3d14553b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d392532422d6f72616e6765) [![PHP 8.1+](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565)](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565)

 [![TOON for Laravel — Compact, token-efficient data for AI workflows](docs/assets/images/hero_banner.png)](docs/assets/images/hero_banner.png)

---

What This Package Is
--------------------

[](#what-this-package-is)

TOON is a Laravel package for converting JSON or PHP arrays into a compact text format that stays readable to humans and efficient for AI-oriented workflows.

It is useful when JSON is too noisy for:

- ChatGPT, Gemini, Claude, Mistral, and OpenAI prompt payloads
- log snapshots and debug output
- internal structured storage or fixture review
- repeated API resource collections with the same fields

Why TOON Helps
--------------

[](#why-toon-helps)

JSON repeats a lot of structure. TOON removes much of that repetition while keeping the data understandable.

 [![JSON vs TOON — 43.1% character savings](docs/assets/images/json_vs_toon_comparison.png)](docs/assets/images/json_vs_toon_comparison.png)

Example input:

```
$payload = [
    'project' => 'TOON',
    'users' => [
        ['id' => 1, 'name' => 'Alice', 'active' => true],
        ['id' => 2, 'name' => 'Bob', 'active' => false],
    ],
];
```

Example TOON output:

```
project: TOON
users:
  items[2]{id,name,active}:
    1,Alice,true
    2,Bob,false

```

Core Features
-------------

[](#core-features)

 [![TOON Core Features — Token Efficient, Round-Trip Safe, Replacer API, Streaming, CLI Tools, LLM-Ready](docs/assets/images/feature_overview.png)](docs/assets/images/feature_overview.png)

Install in 60 Seconds
---------------------

[](#install-in-60-seconds)

```
composer require sbsaga/toon
```

Optional config publishing:

```
php artisan vendor:publish --provider="Sbsaga\Toon\ToonServiceProvider" --tag=config
```

How It Works
------------

[](#how-it-works)

 [![TOON Encode/Decode Pipeline — PHP Array → Toon::encode() → TOON String → Toon::decode() → PHP Array](docs/assets/images/encode_decode_flow.png)](docs/assets/images/encode_decode_flow.png)

Laravel Usage
-------------

[](#laravel-usage)

Facade usage:

```
use Sbsaga\Toon\Facades\Toon;

$toon = Toon::encode($payload);
$decoded = Toon::decode($toon);
$stats = Toon::estimateTokens($toon);
$diff = Toon::diff($payload);
```

Replacer usage:

```
use Sbsaga\Toon\Facades\Toon;

$toon = Toon::encodeWith($payload, function (array $path, string|int|null $key, mixed $value) {
    if ($key === 'debug') {
        return Toon::skip();
    }

    if ($key === 'email') {
        return '[redacted]';
    }

    return $value;
});
```

Global helpers:

```
$toon = toon_encode($payload);
$toonFiltered = toon_encode_with($payload, fn (array $path, string|int|null $key, mixed $value) => $value);
$decoded = toon_decode($toon);
$diff = toon_diff($payload);
```

Collection macro:

```
$toonRows = collect($payload['users'])->toToon();
```

Streaming convenience:

```
$lines = Toon::encodeLines($payload); // Generator
$decoded = Toon::decodeFromLines($lines);
```

CLI conversion (advanced options):

```
php artisan toon:convert storage/example.json --from=json --to=toon --stats
php artisan toon:convert storage/example.toon --decode --strict --pretty
php artisan toon:convert storage/example.json --mode=modern --delimiter=pipe --output=storage/example.toon
```

Laravel Integration Architecture
--------------------------------

[](#laravel-integration-architecture)

 [![TOON Laravel Integration — ServiceProvider, Facade, Encoder, Decoder, Config](docs/assets/images/laravel_integration_architecture.png)](docs/assets/images/laravel_integration_architecture.png)

Backward Compatibility First
----------------------------

[](#backward-compatibility-first)

This release keeps `legacy` compatibility mode as the default so existing projects are less likely to break after upgrade.

 [![Legacy vs Modern Mode — Choose your compatibility path](docs/assets/images/compatibility_modes.png)](docs/assets/images/compatibility_modes.png)

Default stable API:

- `Toon::convert()`
- `Toon::encode()`
- `Toon::decode()`
- `Toon::estimateTokens()`

New optional improvements:

- `Toon::diff()`
- `Toon::convertWith()`
- `Toon::encodeWith()`
- `Toon::skip()`
- `Toon::encodeLines()`
- `Toon::decodeFromLines()`
- `Toon::promptBlock()`
- `Toon::validate()`
- `Toon::contentType()`
- `Toon::fileExtension()`
- `toon_encode()`
- `toon_encode_with()`
- `toon_encode_lines()`
- `toon_decode()`
- `toon_diff()`
- `toon_prompt()`
- `toon_validate()`
- `Collection::toToon()`
- `Sbsaga\Toon\Concerns\Toonable`
- `strict_mode`
- `delimiter`
- `compatibility_mode`

If you want safer nested round trips and cleaner decode behavior for new work, opt into modern mode:

```
// config/toon.php
'compatibility_mode' => 'modern',
```

Reproducible Benchmark
----------------------

[](#reproducible-benchmark)

 [![TOON Benchmark — 43.1% character and token savings vs JSON](docs/assets/images/token_savings_chart.png)](docs/assets/images/token_savings_chart.png)

The repository includes a synthetic benchmark fixture and runner:

- [`benchmarks/fixtures/paginated-users.json`](benchmarks/fixtures/paginated-users.json)
- [`benchmarks/run.php`](benchmarks/run.php)

Run it locally:

```
php benchmarks/run.php benchmarks/fixtures/paginated-users.json
```

Current result for the included synthetic fixture:

MetricJSONTOONCharacters26221492Estimated comparison tokens656373Character savings43.1%Notes:

- the fixture data is synthetic and repository-generated
- the token comparison is a lightweight heuristic for relative comparison
- repeated scalar rows usually benefit most from TOON tables

Why Trust This Package
----------------------

[](#why-trust-this-package)

- Laravel-native facade, service provider, helpers, and collection macro
- backward-safe default compatibility mode for existing users
- opt-in modern mode for cleaner nested-data behavior
- delimiter and strict parsing controls for production use
- benchmark fixture and docs included in the repo
- test coverage for encoder, decoder, helpers, macros, compatibility modes, and edge cases

Documentation
-------------

[](#documentation)

New here: start with [Quickstart](docs/quickstart.md), then [Cookbook: real-world examples](docs/cookbook.md).

Shipping to production: read [Production playbook](docs/production-playbook.md), then [Migration guide](docs/migration.md), and keep [Troubleshooting](docs/troubleshooting.md) handy.

- [Docs index](docs/README.md)
- [Quickstart](docs/quickstart.md)
- [Cookbook: real-world examples](docs/cookbook.md)
- [Production playbook](docs/production-playbook.md)
- [Upgrade safety for v1.3.0](docs/upgrade-safety-v1-3.md)
- [CLI conversion guide](docs/cli-conversion-guide.md)
- [Replacer recipes](docs/replacer-recipes.md)
- [Format and compatibility](docs/spec-compatibility.md)
- [Syntax cheatsheet](docs/syntax-cheatsheet.md)
- [LLM integration guide](docs/llm-integration.md)
- [Media type and files](docs/media-type-and-files.md)
- [When not to use TOON](docs/when-not-to-use-toon.md)
- [Migration guide](docs/migration.md)
- [Benchmarks](docs/benchmarks.md)
- [Use cases](docs/use-cases.md)
- [FAQ](docs/faq.md)
- [Troubleshooting](docs/troubleshooting.md)
- [Reference](docs/reference/README.md)
- [Article index](docs/articles/README.md)

More README Files
-----------------

[](#more-readme-files)

- [Benchmarks README](benchmarks/README.md)
- [Examples README](examples/README.md)
- [Articles README](docs/articles/README.md)
- [Reference README](docs/reference/README.md)

Example Packages and Scenarios
------------------------------

[](#example-packages-and-scenarios)

- [AI prompt compression example](examples/laravel-ai-prompt-compression/README.md)
- [Log payload storage example](examples/laravel-log-payload-storage/README.md)
- [LLM response validation example](examples/llm-response-validation/README.md)
- [HTTP TOON response example](examples/http-toon-response/README.md)
- [Eloquent Toonable trait example](examples/eloquent-toonable/README.md)

Branding Asset
--------------

[](#branding-asset)

- [Social preview source](assets/social-preview.svg)

License
-------

[](#license)

Released under the MIT License.

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance78

Regular maintenance activity

Popularity46

Moderate usage in the ecosystem

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~8 days

Recently: every ~30 days

Total

22

Last Release

122d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/55531496?v=4)[sbsagar](/maintainers/sbsagar)[@sbsagar](https://github.com/sbsagar)

---

Top Contributors

[![sbsaga](https://avatars.githubusercontent.com/u/110087468?v=4)](https://github.com/sbsaga "sbsaga (76 commits)")

---

Tags

aiai-libraryapi-optimizationchatgptcompact-formatdata-compressiondeveloper-toolhuman-readablejsonlaravellaravel-packagelarge-language-modelsllmopenaiphpphp-libraryprompt-engineeringserializationtoken-optimizationtoonjsonlaravelparserailaravel-packageserializationconverteropenaiencoderdecoderstructured-dataGeminiclaudellmChatGptpromptmistrallaravel-aitoontoken-optimizertoken-efficientprompt-compressionlaravel-toontoken-optimizationjson-alternativetoken-savings

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sbsaga-toon/health.svg)

```
[![Health](https://phpackages.com/badges/sbsaga-toon/health.svg)](https://phpackages.com/packages/sbsaga-toon)
```

###  Alternatives

[helgesverre/toon

Token-Oriented Object Notation - A compact data format for reducing token consumption when sending structured data to LLMs

129198.5k36](/packages/helgesverre-toon)[mischasigtermans/laravel-toon

Token-Optimized Object Notation encoder/decoder for Laravel with intelligent nested object handling

14648.4k](/packages/mischasigtermans-laravel-toon)[fomvasss/laravel-ai-tasks

AI task orchestrator for Laravel: routing, queue, audit, budget, webhooks

381.6k1](/packages/fomvasss-laravel-ai-tasks)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
