PHPackages                             diego-ninja/cartoon - 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. diego-ninja/cartoon

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

diego-ninja/cartoon
===================

High-performance TOON (Token-Oriented Object Notation) encoder/decoder for PHP 8.4+

v1.0.0(5mo ago)00MITPHPPHP ^8.4CI passing

Since Nov 19Pushed 5mo agoCompare

[ Source](https://github.com/diego-ninja/cartoon)[ Packagist](https://packagist.org/packages/diego-ninja/cartoon)[ RSS](/packages/diego-ninja-cartoon/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Cartoon - Token-Oriented Object Notation for PHP
================================================

[](#cartoon---token-oriented-object-notation-for-php)

High-performance PHP 8.4+ library for encoding and decoding TOON (Token-Oriented Object Notation) format with full spec compliance.

[![Latest Version on Packagist](https://camo.githubusercontent.com/bfbf23ae1f790f000ebdc97bc55b8f8e8c14cf6518da1dfe66e25a2cfe9124fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646965676f2d6e696e6a612f636172746f6f6e2e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://packagist.org/packages/diego-ninja/cartoon)[![Total Downloads](https://camo.githubusercontent.com/81522d51d4bbaf16a58cd4ac83855724b710fc4d1b11ba15d04c3dbb04638bf1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646965676f2d6e696e6a612f636172746f6f6e2e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://packagist.org/packages/diego-ninja/cartoon)[![PHP Version](https://camo.githubusercontent.com/872b60e615e977b212cb4a7961e4dcd18b5e5c117b245e6f72031230a0b0f92b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646965676f2d6e696e6a612f636172746f6f6e2e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://camo.githubusercontent.com/872b60e615e977b212cb4a7961e4dcd18b5e5c117b245e6f72031230a0b0f92b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646965676f2d6e696e6a612f636172746f6f6e2e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)[![License: MIT](https://camo.githubusercontent.com/a26a213b909339a4120989779b78a923195a74e3915f6e0f0c619bd9f3d23208/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://opensource.org/licenses/MIT)[![GitHub last commit](https://camo.githubusercontent.com/27d564da28e0cd57718dbbdbf5ccb0a8f8a5451c194e3a6192ff2864289dd3c3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f646965676f2d6e696e6a612f636172746f6f6e3f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://camo.githubusercontent.com/27d564da28e0cd57718dbbdbf5ccb0a8f8a5451c194e3a6192ff2864289dd3c3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f646965676f2d6e696e6a612f636172746f6f6e3f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)[![wakatime](https://camo.githubusercontent.com/f9ef73208f3b29ddcf11d2b8a257709dbc830eee52500daffc0fbea39e573a85/68747470733a2f2f77616b6174696d652e636f6d2f62616467652f757365722f62643635663035352d633966332d346637332d393261612d3363393831306637306363332f70726f6a6563742f62623661333131642d653764302d346637632d386539632d6237636434356238616635332e7376673f7374796c653d666c61742d73717561726526636f6c6f723d626c7565266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)](https://wakatime.com/badge/user/bd65f055-c9f3-4f73-92aa-3c9810f70cc3/project/3cc2ec60-a8b4-4ddc-aeac-ea78e37a094b)![Tests](https://camo.githubusercontent.com/573ab7e6946fb17f05a5471144c678c1f2629c80cea8a79c25f23e537ffb234a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646965676f2d6e696e6a612f636172746f6f6e2f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6f676f3d676974687562266c6162656c3d7465737473266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)![Static Analysis](https://camo.githubusercontent.com/8ea4ce00d90eda6f83348be2da76166c53b46d675f534b96b69586b5cd1f4283/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646965676f2d6e696e6a612f636172746f6f6e2f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6f676f3d676974687562266c6162656c3d7068707374616e2532303130266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)![Code Style](https://camo.githubusercontent.com/4d299288103c941d3658ee3524ac3ad109a80ac91de7aa92c35af8e749de7f75/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646965676f2d6e696e6a612f636172746f6f6e2f636f64652d7374796c652e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6f676f3d676974687562266c6162656c3d7374796c653a253230504552266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530)![Coveralls](https://camo.githubusercontent.com/180f8638a7ed5ab4691b7d390401132a071e0e329bbb5d510a2de75192390623/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c73436f7665726167652f6769746875622f646965676f2d6e696e6a612f636172746f6f6e3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6f676f3d636f766572616c6c73266c6f676f436f6c6f723d253233393439636134266c6162656c436f6c6f723d253233336634373530266c696e6b3d6874747073253341253246253246636f766572616c6c732e696f253246676974687562253246646965676f2d6e696e6a61253246636172746f6f6e)

Features
--------

[](#features)

- 🚀 **High Performance**: AST-based parser with JIT optimization
- 🎯 **Spec Compliant**: Full adherence to [TOON specification](https://github.com/toon-format/spec)
- 🔒 **Type Safe**: PHPStan level 10, strict types throughout
- 🧪 **Well Tested**: 95%+ code coverage
- 🎨 **Modern PHP**: Leverages PHP 8.4+ features (readonly, enums, union types)
- 📦 **Zero Dependencies**: No runtime dependencies

Installation
------------

[](#installation)

```
composer require diego-ninja/cartoon
```

**Requirements:** PHP 8.4 or higher

Quick Start
-----------

[](#quick-start)

### Decoding TOON to PHP

[](#decoding-toon-to-php)

```
use Ninja\Cartoon\Toon;

$toon =  true]
```

### Encoding PHP to TOON

[](#encoding-php-to-toon)

```
use Ninja\Cartoon\Toon;

$data = [
    'name' => 'Bob',
    'age' => 25,
    'tags' => ['php', 'toon', 'awesome'],
];

$toon = Toon::encode($data); //or
$toon = toon_encode($data)
```

Output:

```
name: Bob
age: 25
tags[3]: php,toon,awesome

```

Advanced Usage
--------------

[](#advanced-usage)

### Custom Encoding Options

[](#custom-encoding-options)

```
use Ninja\Cartoon\{EncodeOptions,Enum\DelimiterType,Enum\IndentationType,Toon};

$options = new EncodeOptions(
    preferredDelimiter: DelimiterType::Tab,
    indentSize: 4,
    indentationType: IndentationType::Tabs,
    maxCompactArrayLength: 20,
);

$toon = Toon::encode($data, $options);
```

### Custom Decoding Options

[](#custom-decoding-options)

```
use Ninja\Cartoon\{Toon, DecodeOptions};

$options = new DecodeOptions(
    strict: false,              // Allow non-canonical input
    preserveKeyOrder: true,     // Maintain key order
);

$data = Toon::decode($toon, $options);
```

### Strict vs Permissive Mode

[](#strict-vs-permissive-mode)

**Strict mode (default):**

- Validates array lengths exactly
- Requires canonical number format
- Enforces consistent indentation

**Permissive mode:**

- Tolerates array length mismatches
- Accepts non-canonical numbers
- Allows mixed indentation

TOON Format Overview
--------------------

[](#toon-format-overview)

TOON is a line-oriented, indentation-based format encoding the JSON data model:

**Objects:**

```
name: Alice
age: 30

```

**Arrays:**

```
items[3]: a,b,c

```

**Nested structures:**

```
user:
  name: Bob
  address:
    city: NYC

```

**Tabular data:**

```
users[2]{id,name}:
1,Alice
2,Bob

```

See the [official spec](https://github.com/toon-format/spec/blob/main/SPEC.md) for complete details.

Development
-----------

[](#development)

### Setup

[](#setup)

```
git clone https://github.com/diego-ninja/cartoon.git
cd cartoon
composer install
```

### Running Tests

[](#running-tests)

```
# All tests
vendor/bin/phpunit

# With coverage
vendor/bin/phpunit --coverage-html coverage

# Specific test suite
vendor/bin/phpunit tests/Unit
vendor/bin/phpunit tests/Integration
```

### Code Quality

[](#code-quality)

```
# PHPStan (level 10)
vendor/bin/phpstan analyse

# PHP-CS-Fixer (PER coding style)
vendor/bin/php-cs-fixer fix

# All checks
composer test
composer analyze
composer fix
```

Error Handling
--------------

[](#error-handling)

The library provides specific exceptions for different error cases:

- `SyntaxException`: Invalid TOON syntax
- `ValidationException`: Spec violations in strict mode
- `EscapeException`: Invalid escape sequences
- `UnencodableException`: PHP value cannot be encoded (resources, INF, NAN)
- `CircularReferenceException`: Circular reference detected

All exceptions extend `ToonException` for easy catching.

Limitations
-----------

[](#limitations)

- **Multi-line strings**: Not supported (TOON spec doesn't allow them)
- **Special floats**: INF, -INF, NAN cannot be encoded
- **Control characters**: Only `\n`, `\r`, `\t` can be escaped
- **Resources**: Cannot be encoded
- **Closures**: Cannot be encoded

Contributing
------------

[](#contributing)

Contributions welcome! Please:

1. Follow PER coding style
2. Add tests for new features
3. Ensure PHPStan level 10 passes
4. Update documentation as needed

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) file.

Links
-----

[](#links)

- [TOON Specification](https://github.com/toon-format/spec)
- [Issue Tracker](https://github.com/diego-ninja/cartoon/issues)
- [Changelog](CHANGELOG.md)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance70

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

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

Unknown

Total

1

Last Release

173d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dff5b0a76de3d555cb236c400384058ba23982bce2e859cc1b2f0c1f0161a3e6?d=identicon)[diego.ninja](/maintainers/diego.ninja)

---

Top Contributors

[![diego-ninja](https://avatars.githubusercontent.com/u/78662279?v=4)](https://github.com/diego-ninja "diego-ninja (40 commits)")

---

Tags

parseraitokennotationencoderdecoderclaudellmtoon

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/diego-ninja-cartoon/health.svg)

```
[![Health](https://phpackages.com/badges/diego-ninja-cartoon/health.svg)](https://phpackages.com/packages/diego-ninja-cartoon)
```

###  Alternatives

[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).

6115.6k](/packages/sbsaga-toon)[helgesverre/toon

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

11841.4k9](/packages/helgesverre-toon)[mischasigtermans/laravel-toon

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

13113.1k](/packages/mischasigtermans-laravel-toon)[miladrahimi/php-jwt

A PHP implementation of JWT (JSON Web Token) generator, parser, verifier, and validator

70263.2k2](/packages/miladrahimi-php-jwt)[shaarli/netscape-bookmark-parser

Generic Netscape bookmark parser

2643.1k](/packages/shaarli-netscape-bookmark-parser)[suin/json

A Simple wrapper of json\_decode() and json\_encode(). This provides object-oriented interface and exception-based error handing.

1027.6k3](/packages/suin-json)

PHPackages © 2026

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