Quick usage
Basic Encoding (JSON → TOON)
Convert PHP arrays or JSON strings to TOON format:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$data = [
'user' => 'ABC',
'message' => 'Hello, how are you?',
'tasks' => [
['id' => 1, 'done' => false],
['id' => 2, 'done' => true],
],
];
$toon = GeneralUtility::makeInstance(Toon::class)->convert($data);
echo $toon;
Copied!
Output:
user: ABC
message: Hello\, how are you?
tasks:
items[2]{done,id}:
false,1
true,2
Copied!
Basic Decoding (TOON → JSON)
Convert TOON format back to PHP arrays:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$toon = <<<TOON
user: ABC
tasks:
items[2]{id,done}:
1,false
2,true
TOON;
$data = GeneralUtility::makeInstance(Toon::class)->decode($toon);
print_r($data);
Copied!
Output:
Array
(
[user] => ABC
[tasks] => Array
(
[0] => Array
(
[id] => 1
[done] => false
)
[1] => Array
(
[id] => 2
[done] => true
)
)
)
Copied!
Token Estimation
Estimate the number of tokens in a TOON string:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$toonService = GeneralUtility::makeInstance(Toon::class);
$toon = $toonService->convert($data);
$stats = $toonService->estimateTokens($toon);
print_r($stats);
Copied!
Output:
Array
(
[words] => 20
[chars] => 182
[tokens_estimate] => 19
)
Copied!
Using Dependency Injection
For better testability and TYPO3 integration, use dependency injection:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$toonService = GeneralUtility::makeInstance(Toon::class);
$toon = $toonService->convert($data);
$decoded = $toonService->decode($toon);
Copied!
Working with JSON Strings
T3Toon automatically detects and handles JSON strings:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$json = '{"user":"ABC","active":true}';
$toon = GeneralUtility::makeInstance(Toon::class)->convert($json);
echo $toon;
Copied!
Output:
user: ABC
active: true
Copied!
Complex Nested Structures
T3Toon handles deeply nested structures:
use RRP\T3Toon\Service\Toon;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$data = [
'user' => [
'id' => 101,
'active' => true,
'roles' => ['admin', 'editor'],
'profile' => [
'age' => 32,
'location' => [
'city' => 'ABC',
'country' => 'India',
],
],
],
'orders' => [
[
'order_id' => 'ORD-1001',
'amount' => 1998,
'status' => 'paid',
],
],
];
$toon = GeneralUtility::makeInstance(Toon::class)->convert($data);
echo $toon;
Copied!
This structure remains human-readable, reversible, and compact, even with deep nesting.