jobmetric/laravel-toon 问题修复 & 功能扩展

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

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

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

Contributors Forks Stargazers MIT License LinkedIn

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|3 or [2\t]{id\tname}:
  • min_rows_tabular (1): if list of uniform objects has ≥ this count, use tabular format
  • newline_final (false): append trailing newline on encode
  • key_folding ('off'): 'off' or 'safe' — fold single‑key chains into dotted path when safe
  • flatten_depth (-1): max depth of folding (-1 = unlimited)
  • folding_exclude ([]): prefixes to opt‑out from folding
  • expand_paths (false): expand dotted keys on decode (safe segments only)
  • throw_on_decode_error (true): throw on invalid TOON; otherwise return null
  • numbers_as_strings (false): decode numbers as strings instead of numeric types
  • spec_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]:
  • 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
  • Safe Key Folding (optional)
    • key_folding='safe' flattens chains of single‑key nested objects into dot paths when safe; flatten_depth and folding_exclude supported
  • Safe Path Expansion (optional)
    • expand_paths=true expands 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_strings mode
  • First‑Class Laravel Integration
    • Facade Toon::encode / Toon::decode
    • Global helpers toon_encode / toon_decode
    • Container singleton and alias 'Toon' for DI

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 (default 2).

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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