PHP Classes

How to Use a PHP Toon Format Class to Serialize and Unserialized Variable Values Using the Package Toon Parser: Encode and decode values in the TOON format

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-02-01 (21 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
toonparser 1.0MIT/X Consortium ...7Parsers, PHP 7
Description 

Author

This package can encode and decode values in the TOON (Token-Oriented Object Notation) format.

It provides a class that can take a variable value and encodes it to return a string in the TOON format.

The class can also take a string with a value encoded in the TOON format and decodes it to retrieve the original variable value.

It can also convert a value encoded in the TOON format into the JSON format, and vice-versa.

Picture of Juan Camacho
  Performance   Level  
Name: Juan Camacho <contact>
Classes: 11 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Instructions

Please read this document to learn how to encode and decode PHP variable value in the TOON format.

Documentation

TOON Parser for PHP

A lightweight, human-readable data serialization format and parser for PHP.

TOON is designed to be:

  • More readable than JSON
  • More structured than YAML
  • More predictable than custom formats

This library provides a full encoder/decoder for converting between PHP arrays, TOON format, and JSON.

? Features

  • Convert PHP arrays ? TOON
  • Convert TOON ? PHP arrays
  • Convert seamlessly between JSON ? TOON
  • Supports:

    * Primitive values * Nested objects * Lists * Table-like arrays (row-based structured data)

  • Zero dependencies
  • Strict typing (`declare(strict_types=1);`)
  • Fully documented and easy to extend

? Installation

Manual installation

Just include the ToonParser.php file:

require_once 'ToonParser.php';

? What is TOON Format?

TOON is a structured, indentation-based text format.

It supports:

? Objects

config:
  setting1: true
  setting2: "value"

? Lists

users[2]:
  -
    id: 1
    name: Alice
  -
    id: 2
    name: Bob

? Primitive arrays

tags[3]:
  - red
  - green
  - blue

? Tabular data

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

? Example Usage

JSON ? TOON

$json = '{
  "name": "Alice",
  "active": true,
  "roles": ["admin", "editor"]
}';

echo jsonToToon($json);

Output:

name: Alice
active: true
roles[2]:
  - admin
  - editor

TOON ? JSON

$toon = '
user:
  id: 1
  name: "Alice Smith"
  active: true
';

echo toonToJson($toon);

Output:

{
    "user": {
        "id": 1,
        "name": "Alice Smith",
        "active": true
    }
}

? API Documentation

ToonParser

encode(array $data): string

Encodes a PHP array into TOON format.

$parser = new ToonParser();
$toon = $parser->encode($data);

decode(string $toonString): array

Parses a TOON string back into a PHP array.

$array = $parser->decode($toonData);

Helper functions

jsonToToon(string $json): string

Converts a JSON string into TOON.

toonToJson(string $toon): string

Converts TOON into formatted JSON.

? TOON Syntax Rules

? Primitive Types

| TOON | PHP | | -------- | --------- | | true | true | | false | false | | null | null | | numbers | int/float | | "text" | string |

Strings must be quoted only if they contain:

spaces , : { } [ ] newlines tabs

? Indentation

  • Uses two spaces
  • Nested objects depend on indentation
  • No tabs allowed

? Lists

Begin with:

key[count]:

Items start with -.

? Tabular Data

Compact structured rows:

key[count]{col1,col2,col3}:
  value1,value2,value3

? Error Handling

The parser currently assumes valid TOON input. A strict validation mode can be added on request.

? Future Improvements (Roadmap)

  • [ ] Strict validation engine
  • [ ] Streaming encoder/decoder
  • [ ] Syntax highlighter
  • [ ] VSCode extension
  • [ ] Standalone CLI tool
  • [ ] JSON Schema ? TOON Schema converter

? Contributing

Pull requests are welcome. Please open an issue before adding major features.

? License

MIT License ? Free for personal & commercial use.

? About

TOON Parser was designed to provide a readable, predictable data format, bridging the simplicity of JSON and the clarity of indentation-based formats like YAML.


  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file ToonParser.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0