Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Toon Laravel Package

helgesverre/toon

TOON is a compact, human-readable data format that cuts token usage when sending structured data to LLMs. This PHP port offers simple encode and decode methods, works on PHP 8.1+, and supports arrays, objects, and primitives.

View on GitHub
Deep Wiki
Context7
v3.2.1

Changes in v3.2.1

Patch release fixing a decoder round-trip regression introduced in v3.2.0.

Fixed

  • Decoder round-trip regression: quoted string values containing [ or { — for example k: "a[3]b", or log lines carrying ANSI escape sequences such as "…" — were misread as array headers and raised SyntaxException: Unterminated quoted string on decode, so the encoder's own output could not be decoded back. Header detection now locates the : separator and the [/{ markers outside quoted spans, so encoder output round-trips through Toon::decode() for every string value. The regression was introduced in v3.2.0; v3.1.0 was unaffected.

Internal

  • Added comprehensive bidirectional round-trip test coverage (tests/Spec/RoundTripSpecialStringsTest.php, tests/Spec/ComprehensiveRoundTripTest.php, tests/Spec/MalformedHeaderDecodeTest.php); line coverage raised from 90% to 96%.
  • Removed dead code (an orphaned validator cluster and unused DelimiterParser helpers) with no change to the public decode path.

Full Changelog: https://github.com/HelgeSverre/toon-php/compare/v3.2.0...v3.2.1

v3.2.0

Changes in v3.2.0

This release brings the library into conformance with TOON Specification v3.3 (upstream advanced v3.0 → v3.3) and adds a validation API.

Added

  • TOON Specification v3.3 compliance. See docs/SPEC.md and docs/CHANGELOG.md.
  • Unicode escapes (§7.1): the encoder emits C0 control characters (U+0000–U+001F except \n, \r, \t) as \uXXXX; the decoder accepts \uXXXX (case-insensitive hex) and rejects lone surrogates and escapes with fewer than four hex digits.
  • Empty-array decoding (§9.1): the decoder now accepts the canonical [] and key: [] forms in addition to the legacy [0]: / key[0]: forms.
  • Validation API: Toon::validate(), toon_validate() and toon_validate_lenient() for checking TOON syntax without decoding.
  • Spec conformance tests in tests/Spec/Version31To33ComplianceTest.php.

Changed

  • Number formatting (§2): finite numbers use canonical decimal only for n = 0 or 1e-6 ≤ |n| < 1e21; values outside that range use exponent notation (lowercase e, explicit sign, e.g. 1e+21, 1e-7). Large in-domain floats are no longer quoted as strings, and very small numbers no longer underflow to 0.
  • Control characters are no longer rejected on encode — they are escaped and preserved as data (§15).
  • Strict decoding now rejects leading-zero / malformed bracket lengths such as [03] (§6, §14.2), header delimiter mismatches where the bracket delimiter differs from the field-list delimiter, e.g. rows[2|]{a,b}: (§6, §14.2), and duplicate sibling keys at the same depth (§8, §14.4). Non-strict mode treats malformed bracket tokens as literal keys and applies last-write-wins for duplicate keys.

Fixed

  • Decoder: quoted object keys containing colons (e.g. "App\Controller::method") now decode correctly (#4, #5).
  • Decoder: key: [] previously decoded to null and a bare [] crashed with an uninitialized-offset warning; both now decode to an empty array.
  • Decoder — single-line list items (§9.2/§9.4/§10): a list item whose entire content is on the hyphen line now decodes by shape instead of as a plain string — - [M]: … → inner array, - key: … → object, bare - → empty object. This fixes round-trips for arrays of arrays and lists of single-field objects.
  • Decoder — empty document (§5): an empty or whitespace-only document decodes to an empty object in both modes, instead of throwing (strict) or returning null (lenient).
  • Decoder — bare key: (§8): decodes to an empty object, not null.
  • Decoder — quoted header keys (§7.4): a quoted key prefix such as "my-key"[3]: is unescaped.
  • Decoder — empty inline/tabular tokens (§9.1/§11.2): decode to the empty string instead of throwing (e.g. [3]: a,,b → ['a','','b']).
  • Decoder — backslash handling (§7.1/§11.2): an unquoted backslash is a literal character (escapes apply only inside quoted strings), and colon detection tracks quote state so a key ending in an escaped backslash ("a\\": c) parses.
  • Options (§12): EncodeOptions/DecodeOptions reject indent < 1; EncodeOptions::compact() now uses indent: 1 so nested output round-trips.
  • Validation: Toon::validate() now runs the full decoder internally and returns false only when decode() throws, so validation and decoding can never disagree. The separate validator implementation was removed.

Full Changelog: https://github.com/HelgeSverre/toon-php/compare/v3.1.0...v3.2.0

v3.1.0

Added

  • toJSON() support: Objects with a toJSON() method can now provide custom serialization, similar to JSON.stringify in JavaScript

Fixed

  • Decoder now treats negative leading zeros (-05, -0001) as strings per spec §2.4

Changed

  • TOON Specification v3.0 compliance for Section 10 (Objects as List Items)
v3.0.0

Breaking Changes

This major release aligns with TOON Specification v3.0, which standardizes the encoding for list-item objects whose first field is a tabular array.

Changed Behavior (Section 10 - Objects as List Items)

When a list-item object has a tabular array as its first field:

  • Tabular rows: Now appear at depth +2 from the hyphen line (was depth +1)
  • Sibling fields: Remain at depth +1 from the hyphen line

Output Format Change

Before (v2.x):

items[1]:
  - users[2]{id,name}:
    1,Ada
    2,Bob
    status: active

After (v3.0):

items[1]:
  - users[2]{id,name}:
      1,Ada
      2,Bob
    status: active

Migration Notes

  • Encoder output changed: If you rely on exact output format matching, update your expectations
  • Decoder backward compatible: The decoder handles both old and new indentation patterns
  • Round-trip safe: encode → decode → encode produces consistent v3.0 format

Added

  • tests/Spec/Version3BreakingChangesTest.php with 6 tests verifying v3.0 compliance
  • Round-trip test for v3.0 tabular list-item format

Test Coverage

  • Total: 551 tests, 1023 assertions (100% passing)
  • PHPStan Level 9: 0 errors
  • Full TOON Specification v3.0 conformance verified
v2.0.0

Breaking Changes

This major release aligns with TOON Specification v2.0, which removes the optional # length marker prefix from array headers.

Removed Features

  • EncodeOptions::$lengthMarker parameter removed from constructor
  • EncodeOptions::withLengthMarkers() preset method removed
  • EncodeOptions::withLengthMarker() fluent setter removed

Changed Behavior

  • Encoder: Always emits [N] format (e.g., [3]: a,b,c)
  • Decoder: Rejects [#N] format with SyntaxException

Migration Guide

Before (v1.x):

$options = EncodeOptions::withLengthMarkers();
$toon = Toon::encode($data, $options);
// Output: [#3]: a,b,c

After (v2.0):

$options = EncodeOptions::default();
$toon = Toon::encode($data, $options);
// Output: [3]: a,b,c

Fixed

Critical Fixes

  • Locale-dependent float formatting: Fixed sprintf() calls that caused decimal points to become commas in locales like da_DK, de_DE. Now uses number_format() with explicit decimal separator for locale-independent formatting per TOON Spec §2.

Security

  • Control character handling: Strings containing unsupported control characters (0x00-0x08, 0x0B, 0x0C, 0x0E-0x1F) are now rejected with InvalidArgumentException. Only \n, \r, and \t have defined escape sequences per TOON Spec §7.1.

Bug Fixes

  • Decoder error messages: Pattern [N#] now produces correct v2.0 error message

Test Coverage

  • Added 10 tests for v2.0 breaking change verification
  • Added 5 tests for control character rejection
  • Total: 544 tests, 997 assertions (100% passing)
  • PHPStan Level 9: 0 errors

Documentation

  • Updated docs/SPEC-COMPLIANCE.md with control character policy and v2.0 compliance details
  • Full TOON Specification v2.0 conformance verified
v1.4.0

Added

  • Full TOON decoder implementation: Complete decoding functionality with strict mode support
    • Parses all TOON formats: inline arrays, list arrays, tabular arrays, nested objects
    • Strict mode validation (enabled by default) for spec compliance
    • Configurable indentation and delimiter support
    • Comprehensive error handling with specific exception types
    • Round-trip encode/decode verified working perfectly
  • Specification compliance documentation: Created comprehensive SPEC-COMPLIANCE.md
    • Encoder conformance: 10/10 requirements verified
    • Decoder conformance: 7/7 requirements verified
    • Strict mode: 11/11 requirements verified
    • Full TOON Specification v1.3 compliance verified
  • Performance benchmarking: Comprehensive PHPBench suite for performance analysis
    • EncodeBench: Measures encoding performance across data sizes (small, medium, large, xlarge) and format types (inline, tabular, list, nested)
    • DecodeBench: Measures decoding/parsing performance with same variations
    • ThroughputBench: Measures sustained operations per second for realistic workloads
    • ScalabilityBench: Measures performance scaling from 10 to 100K items
    • All benchmarks track execution time and memory usage
  • GitHub Actions workflow: Automated performance benchmarking on every PR
    • Runs benchmarks on PHP 8.1, 8.2, 8.3, 8.4
    • Compares PR performance against main branch baseline
    • Comments results directly on pull requests
    • Detects performance regressions (>15% slower)
    • Stores benchmark results as artifacts
  • Justfile commands: New benchmark commands for local development
    • just benchmark-performance - Run PHPBench with default report
    • just benchmark-perf-summary - Run with summary report
    • just benchmark-all - Run both token efficiency and performance benchmarks
    • just benchmark-baseline - Store current performance as baseline
    • just benchmark-compare - Compare against stored baseline
  • Documentation: Comprehensive README for performance benchmarks explaining metrics, usage, and interpretation
  • Baseline benchmarks: Saved performance baselines before and after encoder refactoring for comparison

Changed

  • Architecture refactoring: Converted Encoder from static methods to instance-based pattern
    • Encoder now stores EncodeOptions and LineWriter as readonly properties
    • Eliminated parameter threading through 9 methods (~30+ parameter passes)
    • Matches Decoder's instance-based architecture for consistency
    • Performance impact is negligible (< 3% worst case, often better)
  • Decoder improvements: Completed Parser instance-based refactoring
    • Fixed remaining DecodeOptions parameter bugs
    • Parser now fully instance-based with stored configuration
    • All 539 tests pass, PHPStan Level 9 clean
v1.3.0

Changes in v1.3.0

Added

Native PHP enum support for both BackedEnum and UnitEnum types:

  • BackedEnum values are extracted and normalized (e.g., Status::ACTIVE -> "active")
  • UnitEnum names are extracted and normalized (e.g., Counting::TWO -> "TWO")
  • Arrays of enum cases are properly encoded (e.g., HttpCode::cases() -> "[2]: 201,400")

Contributors

Thanks to @AmolKumarGupta for contributing this feature!

v1.2.0

Changes in v1.2.0

Fixed

  • Empty array encoding now correctly outputs [0] length marker
    • Before: items:
    • After: items[0]:

Changed

  • README token savings table updated with benchmark data
  • Documentation improvements

Full changelog: https://github.com/HelgeSverre/toon-php/blob/main/CHANGELOG.md#120---2025-10-28

v1.1.0

Changes in v1.1.0

Added

  • Justfile for task automation with commands for setup, testing, analysis, formatting, benchmarks, quality checks, and CI workflows
  • Token efficiency benchmark suite comparing TOON vs JSON vs XML across 4 datasets (GitHub repos, analytics data, e-commerce orders, employee records)
  • Support for Anthropic API token counting or estimation fallback in benchmarks
  • PHPDoc documentation for Toon::encode() method
  • Extended test coverage with 3 new test files

Changed

  • Benchmark output formatting simplified to use clean, minimal text instead of decorative boxes
  • README enhanced with additional examples

Benchmark Results

Token savings compared to other formats:

  • GitHub Repositories: 46.7% vs JSON, 61.4% vs XML
  • Analytics Data: 68.0% vs JSON, 81.4% vs XML
  • E-Commerce Orders: 29.6% vs JSON, 54.3% vs XML
  • Employee Records: 56.7% vs JSON, 70.6% vs XML

Full changelog: https://github.com/HelgeSverre/toon-php/blob/main/CHANGELOG.md#110---2025-10-28

v1.0.0

Initial Stable Release

TOON (Token-Oriented Object Notation) is a compact data format for reducing token consumption when sending structured data to Large Language Models.

Installation

composer require helgesverre/toon

Example

use HelgeSverre\Toon\Toon;

echo Toon::encode([
    'items' => [
        ['sku' => 'A1', 'qty' => 2, 'price' => 9.99],
        ['sku' => 'B2', 'qty' => 1, 'price' => 14.5]
    ]
]);

Output:

items[2]{sku,qty,price}:
  A1,2,9.99
  B2,1,14.5

Features

  • Support for primitive types (strings, numbers, booleans, null)
  • Objects with key-value pairs
  • Arrays in multiple formats (primitive, tabular, nested)
  • DateTime objects in ISO 8601 format
  • Intelligent string quoting
  • Configurable indentation and delimiters
  • 133 tests, 181 assertions

Documentation

Full changelog: https://github.com/HelgeSverre/toon-php/commits/v1.0.0

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core