Download the PHP package mischasigtermans/laravel-toon without Composer
On this page you can find all versions of the php package mischasigtermans/laravel-toon. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mischasigtermans/laravel-toon
More information about mischasigtermans/laravel-toon
Files in mischasigtermans/laravel-toon
Package laravel-toon
Short Description Token-Optimized Object Notation encoder/decoder for Laravel with intelligent nested object handling
License MIT
Informations about the package laravel-toon
Laravel TOON
The most complete TOON implementation for Laravel, and the only one with full TOON v3.0 specification compliance. Listed as official Laravel implementation.
TOON (Token-Optimized Object Notation) is a compact, YAML-like format designed to reduce token usage when sending data to LLMs. This package achieves ~50% token reduction compared to JSON while maintaining full round-trip fidelity, backed by 470 tests.
Installation
Quick Start
Output:
Global Helper Functions
For convenience, global helper functions are available:
Collection Macro: toToon
You can convert any Laravel collection directly to TOON format with the built-in toToon macro:
Output:
Eloquent Model: toToon
Eloquent models have a toToon method, similar to toJson and toArray:
Why TOON?
When building MCP servers or LLM-powered applications, every token counts. JSON's verbosity wastes context window space with repeated keys and structural characters.
JSON (201 bytes):
TOON (62 bytes) - 69% smaller:
Benchmarks
For a typical paginated API response (50 records):
- JSON: ~7,597 tokens
- TOON: ~3,586 tokens
- Saved: ~4,000 tokens per request
Real-world benchmarks from a production application with 17,000+ records:
| Data Type | JSON | TOON | Savings |
|---|---|---|---|
| 50 records | 30,389 bytes | 14,343 bytes | 53% |
| 100 records | 60,856 bytes | 28,498 bytes | 53% |
| 500 records | 303,549 bytes | 140,154 bytes | 54% |
| 1,000 records | 604,408 bytes | 277,614 bytes | 54% |
Features
Tabular Format
Arrays of uniform objects with primitive values are encoded as compact tables:
List Format for Nested Objects
Arrays containing objects with nested properties use list format for clarity:
Type Preservation
All scalar types are preserved through encode/decode:
String Quoting (Spec-Compliant)
Strings containing special characters are automatically quoted per the TOON spec:
Safe strings (alphanumeric, underscores, dots) remain unquoted for minimal overhead.
Configuration
Publish the config file:
Basic Options
Token-Saving Options
Value Transformation
Utility Methods
Measure Savings
Encode Specific Keys Only
Use Cases
MCP Servers
Reduce token usage when returning data from MCP tool calls:
LLM Context
Pack more data into your context window:
API Responses
Optional TOON responses for token-conscious clients:
Spec Compliance
This package implements the TOON v3.0 specification and passes the official specification test suite. Key compliance features:
- String quoting: Safe strings unquoted, special characters properly escaped (
\n,\r,\t,\",\\) - Delimiter support: Comma (default), tab, and pipe delimiters
- Tabular format: Compact tables for arrays of primitive-only objects (
[N]{fields}:) - List format: Readable structure for arrays with nested objects (
[N]:with- field:items) - Inline arrays: Primitive arrays on single line (
key[N]: a,b,c) - Strict mode: Optional validation during decoding
- Backward compatibility: Decoder accepts legacy formats (backslash escaping, dot-notation columns)
Testing
The test suite includes 470 tests covering encoding, decoding, nested object handling, and official spec compliance fixtures.
Requirements
- PHP 8.2+
- Laravel 10, 11, or 12
Credits
License
MIT