DownloadTOON 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
? 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.
|