sbsaga/toon 问题修复 & 功能扩展

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

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

sbsaga/toon

Composer 安装命令:

composer require sbsaga/toon

包简介

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts & LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

README 文档

README

Compact, human-readable, and token-efficient structured data for AI prompts, logs, and Laravel workflows

TOON logo

Latest version Total downloads Tests License Laravel 9+ PHP 8.1+

TOON for Laravel — Compact, token-efficient data for AI workflows

What This Package Is

TOON is a Laravel package for converting JSON or PHP arrays into a compact text format that stays readable to humans and efficient for AI-oriented workflows.

It is useful when JSON is too noisy for:

  • ChatGPT, Gemini, Claude, Mistral, and OpenAI prompt payloads
  • log snapshots and debug output
  • internal structured storage or fixture review
  • repeated API resource collections with the same fields

Why TOON Helps

JSON repeats a lot of structure. TOON removes much of that repetition while keeping the data understandable.

JSON vs TOON — 43.1% character savings

Example input:

$payload = [
    'project' => 'TOON',
    'users' => [
        ['id' => 1, 'name' => 'Alice', 'active' => true],
        ['id' => 2, 'name' => 'Bob', 'active' => false],
    ],
];

Example TOON output:

project: TOON
users:
  items[2]{id,name,active}:
    1,Alice,true
    2,Bob,false

Core Features

TOON Core Features — Token Efficient, Round-Trip Safe, Replacer API, Streaming, CLI Tools, LLM-Ready

Install in 60 Seconds

composer require sbsaga/toon

Optional config publishing:

php artisan vendor:publish --provider="Sbsaga\Toon\ToonServiceProvider" --tag=config

How It Works

TOON Encode/Decode Pipeline — PHP Array → Toon::encode() → TOON String → Toon::decode() → PHP Array

Laravel Usage

Facade usage:

use Sbsaga\Toon\Facades\Toon;

$toon = Toon::encode($payload);
$decoded = Toon::decode($toon);
$stats = Toon::estimateTokens($toon);
$diff = Toon::diff($payload);

Replacer usage:

use Sbsaga\Toon\Facades\Toon;

$toon = Toon::encodeWith($payload, function (array $path, string|int|null $key, mixed $value) {
    if ($key === 'debug') {
        return Toon::skip();
    }

    if ($key === 'email') {
        return '[redacted]';
    }

    return $value;
});

Global helpers:

$toon = toon_encode($payload);
$toonFiltered = toon_encode_with($payload, fn (array $path, string|int|null $key, mixed $value) => $value);
$decoded = toon_decode($toon);
$diff = toon_diff($payload);

Collection macro:

$toonRows = collect($payload['users'])->toToon();

Streaming convenience:

$lines = Toon::encodeLines($payload); // Generator<string>
$decoded = Toon::decodeFromLines($lines);

CLI conversion (advanced options):

php artisan toon:convert storage/example.json --from=json --to=toon --stats
php artisan toon:convert storage/example.toon --decode --strict --pretty
php artisan toon:convert storage/example.json --mode=modern --delimiter=pipe --output=storage/example.toon

Laravel Integration Architecture

TOON Laravel Integration — ServiceProvider, Facade, Encoder, Decoder, Config

Backward Compatibility First

This release keeps legacy compatibility mode as the default so existing projects are less likely to break after upgrade.

Legacy vs Modern Mode — Choose your compatibility path

Default stable API:

  • Toon::convert()
  • Toon::encode()
  • Toon::decode()
  • Toon::estimateTokens()

New optional improvements:

  • Toon::diff()
  • Toon::convertWith()
  • Toon::encodeWith()
  • Toon::skip()
  • Toon::encodeLines()
  • Toon::decodeFromLines()
  • Toon::promptBlock()
  • Toon::validate()
  • Toon::contentType()
  • Toon::fileExtension()
  • toon_encode()
  • toon_encode_with()
  • toon_encode_lines()
  • toon_decode()
  • toon_diff()
  • toon_prompt()
  • toon_validate()
  • Collection::toToon()
  • Sbsaga\Toon\Concerns\Toonable
  • strict_mode
  • delimiter
  • compatibility_mode

If you want safer nested round trips and cleaner decode behavior for new work, opt into modern mode:

// config/toon.php
'compatibility_mode' => 'modern',

Reproducible Benchmark

TOON Benchmark — 43.1% character and token savings vs JSON

The repository includes a synthetic benchmark fixture and runner:

Run it locally:

php benchmarks/run.php benchmarks/fixtures/paginated-users.json

Current result for the included synthetic fixture:

Metric JSON TOON
Characters 2622 1492
Estimated comparison tokens 656 373
Character savings 43.1%

Notes:

  • the fixture data is synthetic and repository-generated
  • the token comparison is a lightweight heuristic for relative comparison
  • repeated scalar rows usually benefit most from TOON tables

Why Trust This Package

  • Laravel-native facade, service provider, helpers, and collection macro
  • backward-safe default compatibility mode for existing users
  • opt-in modern mode for cleaner nested-data behavior
  • delimiter and strict parsing controls for production use
  • benchmark fixture and docs included in the repo
  • test coverage for encoder, decoder, helpers, macros, compatibility modes, and edge cases

Documentation

New here: start with Quickstart, then Cookbook: real-world examples.

Shipping to production: read Production playbook, then Migration guide, and keep Troubleshooting handy.

More README Files

Example Packages and Scenarios

Branding Asset

License

Released under the MIT License.

sbsaga/toon 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 49.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 67
  • 点击次数: 46
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 64
  • Watchers: 1
  • Forks: 8
  • 开发语言: PHP

其他信息

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