zonuexe/broken-json
Composer 安装命令:
composer require zonuexe/broken-json
包简介
README 文档
README
Repair and decode broken JSON in PHP, with a focus on truncated payloads from logs and storage.
Why This Exists
In real systems, large JSON payloads can be cut off (for example, DB column limits or transport issues). This library tries to recover those payloads into usable PHP values and gives you repair metadata.
Quick Start
<?php use zonuexe\BrokenJson\DecoderFactory; $json = '{"foo": {"name": "foo", "bar": {"buz": ["text data", "huge.........text'; $decoder = DecoderFactory::create(); $result = $decoder->decodeString($json); // decoded value after repair $data = $result->value; #=> ['foo' => ['name' => 'foo', 'bar' => ['buz' => ['text data', 'huge.........text']]]] // recovery metadata $isRecovered = $result->isRecovered; #=> true $isPartial = $result->isPartial; #=> true $repairs = $result->repairs; #=> [RepairAction(type: close_string), RepairAction(type: close_container), ...] $issues = $result->issues; #=> []
API
DecoderFactory::create(?DecodeOptions $options = null, ?Utf8Sanitizer $utf8Sanitizer = null): DecoderDecoder::decodeString(string $json): DecodeResultDecoder::decodeChunks(iterable<string> $chunks): DecodeResultDecoder::decodeStream(resource $stream): DecodeResultDecoder::decodeFile(string $path): DecodeResultDecoder::decode(string $json): list{mixed, list<RepairAction>}(legacy compatibility)
UTF-8 Sanitization
DecoderFactory auto-selects a UTF-8 sanitizer backend in this order:
MbstringUtf8Sanitizer(mb_scrub)IconvUtf8Sanitizer(iconv)
If neither backend is available, factory creation fails with LogicException.
You can also inject your own sanitizer implementation:
<?php use zonuexe\BrokenJson\DecoderFactory; use zonuexe\BrokenJson\Internal\Utf8Sanitizer; $sanitizer = new class () implements Utf8Sanitizer { public function sanitize(string $value): string { return $value; } }; $decoder = DecoderFactory::create(utf8Sanitizer: $sanitizer);
Result Object
DecodeResult includes:
value: decoded PHP value (ornullif decoding still fails)isRecovered: whether any repair was appliedisPartial: whether the result may be partial/incompleterepairs: list of applied repair actionsissues: list of decoding/recovery issuesrepairedJson: repaired JSON string used for final decode
JSON Serialization
DecodeResult implements JsonSerializable, so it can be passed directly to json_encode().
$json = json_encode($result, JSON_THROW_ON_ERROR); #=> {"value":{"foo":{"name":"foo","bar":{"buz":["text data","huge.........text"]}}},"isRecovered":true,...}
Fuzzing
This repository includes a php-fuzzer target for crash-oriented stress testing.
make fuzz #=> runs bounded fuzzing (default: 5000 runs, 5s timeout/input) #=> override: make fuzz FUZZ_MAX_RUNS=20000 FUZZ_TIMEOUT=3
To replay or minimize a crash input:
make fuzz-single INPUT=crash-xxxx.txt #=> executes one input against the fuzz target make fuzz-minimize INPUT=crash-xxxx.txt #=> creates minimized-*.txt candidates
Repair Policy
Use DecodeOptions to control behavior:
conservative: minimal safe repairsbalanced(default): practical repairs for common truncation casesaggressive: allows more permissive repairs
Copyright
This package is licensed under the MPL 2.0. See LICENSE file.
Copyright (C) 2026 USAMI Kenta
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
zonuexe/broken-json 适用场景与选型建议
zonuexe/broken-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zonuexe/broken-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zonuexe/broken-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2026-02-26