PHP code example of mateuszkardas / toon
1. Go to this page and download the library: Download mateuszkardas/toon library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mateuszkardas / toon example snippets
oon\Toon;
// Data to encode
$data = [
'users' => [
['id' => 1, 'name' => 'Alice', 'role' => 'admin'],
['id' => 2, 'name' => 'Bob', 'role' => 'user'],
],
];
// Encode to TOON
$encoded = Toon::encode($data);
echo $encoded;
// Output:
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// Decode back to PHP
$decoded = Toon::decode($encoded);
print_r($decoded);
Toon::encode(mixed $input, ?EncodeOptions $options = null): string
use Toon\Toon;
use Toon\EncodeOptions;
use Toon\Constants;
$options = new EncodeOptions(
indent: 4,
delimiter: Constants::DELIMITER_PIPE,
lengthMarker: '#'
);
$result = Toon::encode($data, $options);
Toon::decode(string $input, ?DecodeOptions $options = null): mixed
use Toon\Toon;
use Toon\DecodeOptions;
$options = new DecodeOptions(
indent: 4,
strict: false
);
$result = Toon::decode($toonString, $options);
// Shorthand for encode with defaults
$toon = Toon::stringify($data);
// Shorthand for decode with defaults
$data = Toon::parse($toonString);
$data = [
'name' => 'Alice',
'age' => 30,
'active' => true,
'deleted' => null,
];
// TOON:
// name: Alice
// age: 30
// active: true
// deleted: null
$data = [
'user' => [
'name' => 'Alice',
'email' => '[email protected] ',
],
];
// TOON:
// user:
// name: Alice
// email: [email protected]
$data = [
'tags' => ['php', 'toon', 'serialization'],
'scores' => [95, 87, 92],
];
// TOON:
// tags[3]: php,toon,serialization
// scores[3]: 95,87,92
$data = [
'users' => [
['id' => 1, 'name' => 'Alice', 'role' => 'admin'],
['id' => 2, 'name' => 'Bob', 'role' => 'user'],
['id' => 3, 'name' => 'Charlie', 'role' => 'user'],
],
];
// TOON:
// users[3]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// 3,Charlie,user
$data = [
'items' => [
['name' => 'Item A', 'nested' => ['x' => 1]],
['name' => 'Item B', 'nested' => ['x' => 2]],
],
];
// TOON:
// items[2]:
// - name: Item A
// nested:
// x: 1
// - name: Item B
// nested:
// x: 2
$data = [
'text' => "Line 1\nLine 2\tTabbed",
'quoted' => 'She said "Hello"',
'path' => 'C:\\Users\\file.txt',
];
// TOON:
// text: "Line 1\nLine 2\tTabbed"
// quoted: "She said \"Hello\""
// path: "C:\\Users\\file.txt"
use Toon\Constants;
// Pipe delimiter
$options = new EncodeOptions(delimiter: Constants::DELIMITER_PIPE);
// Output: users[2]{id,name}:
// 1|Alice
// 2|Bob
// Tab delimiter
$options = new EncodeOptions(delimiter: Constants::DELIMITER_TAB);
// Output: users[2]{id,name}:
// 1 Alice
// 2 Bob
$options = new EncodeOptions(lengthMarker: '#');
// Output: users[#2]{id,name}:
// 1,Alice
// 2,Bob
bash
php examples/basic.php
php examples/advanced.php