phpiando/toonify 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpiando/toonify

Composer 安装命令:

composer require phpiando/toonify

包简介

A PHP library to convert between JSON and TOON format for optimized LLM data exchange.

README 文档

README

PHP library for conversion between JSON and TOON (Token-Oriented Object Notation) - an optimized format for saving tokens in Large Language Models (LLMs).

PHP Version License

☕ Sponsors

If you find this plugin useful, consider sponsoring its development:

📖 What is TOON?

TOON (Token-Oriented Object Notation) is a compact and readable serialization format, specifically designed to reduce token usage when sending structured data to LLMs. It can save 30-60% of tokens compared to JSON.

Why use TOON?

  • 💸 Token savings: 30-60% fewer tokens than JSON
  • 🤖 Optimized for LLMs: Better understanding and accuracy
  • 📊 Perfect for tabular data: Uniform arrays of objects
  • 🔄 Lossless conversion: Converts to and from JSON without data loss

Quick Comparison

JSON (123 tokens):

{
  "title": "Employees",
  "total": 3,
  "data": [
    {
      "id": 1,
      "name": "Roni",
      "email": "roni@phpiando.com"
    },
    {
      "id": 2,
      "name": "Sommerfeld",
      "email": "sommerfeld@phpiando.com"
    },
    {
      "id": 3,
      "name": "PHPiando",
      "email": "phpiando@phpiando.com"
    }
  ]
}

TOON (64 tokens - 48% savings):

title: Employees
total: 3
data[3]{id,name,email}:
  1,Roni,roni@phpiando.com
  2,Sommerfeld,sommerfeld@phpiando.com
  3,PHPiando,phpiando@phpiando.com

🚀 Installation

composer require phpiando/toonify

Requirements

  • PHP 8.3 or higher
  • ext-json
  • ext-mbstring

📚 Basic Usage

Simple Conversion

use Toonify\Toon;

// PHP Array to TOON
$data = [
  'title' => 'Employees',
  'total' => 3,
  'data' => [
    ['id' => 1, 'name' => 'Roni', 'email' => 'roni@phpiando.com'],
    ['id' => 2, 'name' => 'Sommerfeld', 'email' => 'sommerfeld@phpiando.com'],
    ['id' => 3, 'name' => 'PHPiando', 'email' => 'phpiando@phpiando.com'],
  ]
];

$toon = Toonify::encode($data);
echo $toon;
// title: Employees
// total: 3
// data[3]{id,name,email}:
//   1,Roni,roni@phpiando.com
//   2,Sommerfeld,sommerfeld@phpiando.com
//   3,PHPiando,phpiando@phpiando.com

// TOON to PHP Array
$decoded = Toonify::decode($toon);

From JSON String

$json = '{"name": "Roni Sommerfeld", "age": 37}';
$toon = Toonify::fromJsonString($json);

$jsonBack = Toonify::toJsonString($toon);

From Local Files

// JSON → TOON
$toon = Toonify::fromJsonDisk('/path/to/data.json');
file_put_contents('/path/to/output.toon', $toon);

// TOON → JSON
$json = Toonify::toJsonDisk('/path/to/data.toon');
file_put_contents('/path/to/output.json', $json);

From URLs

// Fetch JSON from URL and convert to TOON
$toon = Toonify::fromJsonUrl('https://api.example.com/data.json');

// Fetch TOON from URL and convert to JSON
$json = Toonify::toJsonUrl('https://example.com/data.toon');

Extract TOON from Markdown (LLM Responses)

A special feature for working with LLM responses that frequently return data in markdown blocks:

$llmResponse = <<<'MARKDOWN'
Here is the data:

```toon
users[2]{id,name}:
  1,Roni
  2,PHPiando

Hope this helps! MARKDOWN;

// Extract only TOON content $toon = Toonify::extractFromMarkdown($llmResponse);

// Or convert directly to JSON $json = Toonify::toJsonString($toon);


## ⚙️ Configuration Options

### Encoding Options

```php
$toon = Toonify::encode($data, [
  'delimiter' => ',',      // ',', "\t" or '|'
  'indent' => 2,           // Spaces per level
  'lengthMarker' => '',    // Length prefix (optional)
]);

Delimiter examples:

// Comma (default)
$toon = Toonify::encode($data, ['delimiter' => ',']);
// users[2,]{id,name}:

// Tab (more token efficient)
$toon = Toonify::encode($data, ['delimiter' => "\t"]);
// users[2	]{id,name}:

// Pipe
$toon = Toonify::encode($data, ['delimiter' => '|']);
// users[2|]{id,name}:

Decoding Options

$data = Toonify::decode($toon, [
  'strict' => true,        // Strict validation (default: true)
  'indent' => 2,           // Expected spaces per level
]);

📋 Supported Formats

Simple Objects

['name' => 'Roni Sommerfeld', 'age' => 37]
name: Roni Sommerfeld
age: 37

Primitive Arrays

[1, 2, 3, 4, 5]
[5,]: 1,2,3,4,5

Tabular Arrays (Sweet Spot!)

[
  ['id' => 1, 'name' => 'Roni'],
  ['id' => 2, 'name' => 'PHPiando']
]
[2,]{id,name}:
  1,Roni
  2,PHPiando

Mixed Arrays

[
  ['x' => 1],
  42,
  'hello'
]
[3,]:
  - x: 1
  - 42
  - hello

🎯 Use Cases

1. Send data to LLMs

$repositories = fetchGitHubRepos();
$toon = Toonify::encode($repositories, ['delimiter' => "\t"]);

// Save tokens in prompt
$prompt = "Analyze these repositories:\n\n" . $toon;
$response = $llm->complete($prompt);

2. Process LLM responses

$response = $llm->complete("List 5 products in TOON format");
$toon = Toonify::extractFromMarkdown($response);
$products = Toonify::decode($toon);

3. Optimized APIs

// Endpoint that returns TOON instead of JSON
header('Content-Type: text/plain');
$data = $database->query('SELECT * FROM users');
echo Toonify::encode($data);

4. Compact logs

$logData = [
  'timestamp' => time(),
  'events' => $events
];
file_put_contents('log.toon', Toonify::encode($logData));

🧪 Testing

composer test

📊 Benchmarks

Run examples to see token savings:

php examples/basic.php

Typical results:

  • Simple objects: ~20-30% savings
  • Tabular arrays: ~40-60% savings
  • Mixed arrays: ~25-35% savings

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/MyFeature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/MyFeature)
  5. Open a Pull Request

📄 License

This project is under the MIT license. See the LICENSE file for more details.

🔗 Useful Links

🙏 Credits

TOON was created by Johann Schopplich.

This PHP library is an implementation of the official TOON v1.3 specification.

💬 Support

Made with ❤️ for the PHP community

phpiando/toonify 适用场景与选型建议

phpiando/toonify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「serialization」 「ai」 「llm」 「toon」 「token-optimization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpiando/toonify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpiando/toonify 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpiando/toonify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-10