jobmetric/laravel-toon
Composer 安装命令:
composer require jobmetric/laravel-toon
包简介
Laravel integration for TOON format: encode/decode JSON data into a token-optimized notation for LLMs.
关键字:
README 文档
README
Laravel Toon Converter (json to toon for LLMs)
Laravel integration for TOON format: encode/decode JSON data into a token‑optimized notation for LLMs. This package provides first‑class TOON support in Laravel with spec‑conformant encoding/decoding, smart tabular formatting, safe key folding, and developer‑friendly APIs (Facade, helpers, DI).
Requirements
- PHP >= 8.0.1
- Laravel >= 9.19
- Composer
Installation
Install via Composer:
composer require jobmetric/laravel-toon
The service provider is auto‑discovered. The Toon facade and global helpers are registered.
Configuration
Publish/override the laravel-toon config as needed. Available options (defaults in parentheses):
indent(2): spaces per indent level (encoder/decoder)delimiter(','): array delimiter — one of,,\t,|- Non‑default delimiter appears in headers, e.g.
[3|]: 1|2|3or[2\t]{id\tname}:
- Non‑default delimiter appears in headers, e.g.
min_rows_tabular(1): if list of uniform objects has ≥ this count, use tabular formatnewline_final(false): append trailing newline on encodekey_folding('off'): 'off' or 'safe' — fold single‑key chains into dotted path when safeflatten_depth(-1): max depth of folding (-1= unlimited)folding_exclude([]): prefixes to opt‑out from foldingexpand_paths(false): expand dotted keys on decode (safe segments only)throw_on_decode_error(true): throw on invalid TOON; otherwise returnnullnumbers_as_strings(false): decode numbers as strings instead of numeric typesspec_strict(true): prefer strict spec behavior (no trailing newline, stricter normalization)
Features
- Spec‑Conformant Headers
- Primitive arrays:
[n]: v1,v2,v3 - Tabular arrays:
[n]{k1,k2}:then rows indented one level - Delimiter suffix for non‑default delimiters in headers:
[n|],[n\t] - Empty arrays:
[0]:
- Primitive arrays:
- Smart Tabular Encoding
- Detects uniform lists of objects; emits compact tabular blocks with header fields
- Human‑readable and token‑efficient (ideal for LLM prompts)
- Mixed Arrays with Nested Items
- Renders
[n]:then list items with-, and handles nested multi‑line blocks with correct indentation
- Renders
- Safe Key Folding (optional)
key_folding='safe'flattens chains of single‑key nested objects into dot paths when safe;flatten_depthandfolding_excludesupported
- Safe Path Expansion (optional)
expand_paths=trueexpands dotted keys into nested objects on decode (safe segments only)
- Robust Decoding and Validation
- Strict indentation checks; array length and tabular row count validation
- Configurable error handling via
throw_on_decode_error
- Predictable String/Numeric Handling
- Spec‑style quoting/escaping; optional
numbers_as_stringsmode
- Spec‑style quoting/escaping; optional
- First‑Class Laravel Integration
- Facade
Toon::encode/Toon::decode - Global helpers
toon_encode/toon_decode - Container singleton and alias
'Toon'for DI
- Facade
Usage
Facade
use JobMetric\Toon\Facades\Toon; $data = [ 'id' => 42, 'tags' => ['x', 'y'], 'users' => [ ['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob'], ], ]; $toon = Toon::encode($data); // [2]{id,name}: // 1,Alice // 2,Bob $decoded = Toon::decode($toon); // same as $data
Helpers
$encoded = toon_encode(['numbers' => [1, 2, 3]]); // [3]: 1,2,3 $decoded = toon_decode($encoded);
Container/DI
use JobMetric\Toon\ToonManager; $toon = app(ToonManager::class)->encode(['items' => ['a', 'b']]); // items[2]: a,b
Custom Delimiter
config()->set('laravel-toon.delimiter', '|'); echo Toon::encode(['nums' => [1,2,3]]); // nums[3|]: 1|2|3
Safe Key Folding + Path Expansion
config()->set('laravel-toon.key_folding', 'safe'); config()->set('laravel-toon.expand_paths', true); $encoded = Toon::encode(['a' => ['b' => ['c' => 1]]]); // a.b.c: 1 $decoded = Toon::decode($encoded); // ['a' => ['b' => ['c' => 1]]]
Error Handling
config()->set('laravel-toon.throw_on_decode_error', false); $decoded = toon_decode("id:\n name: Alice\n"); // null (no exception)
Numbers as Strings
config()->set('laravel-toon.numbers_as_strings', true); $decoded = Toon::decode('n: 12345678901234567890'); // ['n' => '12345678901234567890']
Spec Notes (Parity)
- Headers only include delimiter suffix when non‑default (
,is implicit):[3|],[2\t]. - Tabular header fields are delimited using the same active delimiter.
- Strings are quoted only when necessary (whitespace, delimiter present, structural chars, looks like literal/number, control chars, backslash).
- Keys are unquoted when safe; dotted keys allowed when each segment is a safe identifier.
- Decoder enforces indentation width to be multiples of
indent(default2).
Why jobmetric/laravel-toon
- Spec‑accurate TOON for Laravel (delimiter‑aware headers, robust parsing)
- Token‑efficient formatting — especially tabular encoding
- Safety controls (folding/expand for safe identifiers, strict indentation)
- Predictable numeric/string handling (optional strings for large numbers)
- Developer experience: Facade, helpers, alias, simple config
Contributing
Thank you for considering contributing to Laravel Toon! Please see CONTRIBUTING.md.
License
The MIT License (MIT). See LICENCE.md for details.
jobmetric/laravel-toon 适用场景与选型建议
jobmetric/laravel-toon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 437 次下载、GitHub Stars 达 26, 最近一次更新时间为 2025 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「json」 「laravel」 「prompt」 「ai」 「structured-data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jobmetric/laravel-toon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jobmetric/laravel-toon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jobmetric/laravel-toon 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
JPOPHP (JSON Parser Object PHP) is a library for parsing the data in JSON format.
统计信息
- 总下载量: 437
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 26
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-17