aternos/almost-json
Composer 安装命令:
composer require aternos/almost-json
包简介
Parse all those weird things that should be JSON but someone decided to add weird quirks
README 文档
README
A library to parse all those things that are kind of JSON but not quite.
Why?
Over the years, many accidentally or intentionally broken JSON parsers have emerged.
If a software uses such a parser, users of that software might start writing broken JSON
that is accepted by that parser, but not actually valid.
This makes writing code that deals with files originally created for such software very
annoying, since some files will just randomly not work with a standard JSON parser because
someone decided that wrapping a string in quotes was just way too much work for them.
This library aims to correctly parse normal JSON, while also accommodating as many weird quirks as possible.
Supported JSON quirks
When parsing valid JSON, this library should return the exact same result as PHP's built-in
json_decode function. Additionally, the following quirks are supported:
- Line comments (
//and#) - Block comments (
/* ... */) - Objects
- Unquoted keys
- Missing or additional commas
- Implicit close at end of input
- Arrays
- Missing or additional commas
- Implicit close at end of input
- Strings
- Single quotes (
') instead of double quotes (") - Backticks (
`) instead of double quotes (") - Unquoted strings
- Escape sequences
\vand\0 - Ignore invalid escape sequences (e.g.
\a=>a) - Line continuation (
\at the end of a line) - Literal newlines
- Single quotes (
- Numbers
- Hexadecimal numbers (
0x...) - Binary numbers (
0b...) - Octal numbers (
0o...) - Octal numbers with only
0as prefix (0...) (not enabled by default) - Allow leading zeros (e.g.
0123) for decimal numbers if octal numbers are enabled - Exponential notation with
eorE - Underscores in numbers (e.g.
1_000_000) - Decimal numbers with a trailing dot (e.g.
1.) - Decimal numbers with a leading dot (e.g.
.1) - Float values
NaN,Infinity, and-Infinity
- Hexadecimal numbers (
- Case-insensitive boolean values
- Case-insensitive null
- Ignore additional data after end of root node
Usage
Installation
composer require aternos/almost-json
Parsing
$parser = new \Aternos\AlmostJson\AlmostJsonParser(); $parsed = $parser->parseString('{key: "value"}', assoc: true); // => ['key' => 'value']
By default, the parser will assume the string is encoded as UTF-8. To use a different encoding
you can call the parse method, which accepts an Input object instead of a string.
$parser = new \Aternos\AlmostJson\AlmostJsonParser(); $input = new \Aternos\AlmostJson\Input('{key: "value"}', encoding: "ISO-8859-1"); $parsed = $parser->parse($input, assoc: true);
To automatically detect the correct encoding, you can pass the result of the mb_detect_encoding function.
Options
There are a number of options that can be set for a parser instance.
$parser = new \Aternos\AlmostJson\AlmostJsonParser(); $parser->setMaxDepth(512) // Maximum depth of the JSON tree ->setZeroPrefixOctal(true) // Parse numbers with leading zero as octal ->setTopLevelUnquotedStringAllowed(true); // Allow the root of the JSON tree to be an unquoted string
Performance
This library is almost certainly significantly slower than PHP's built-in JSON implementation. If performance is critical, and most files are expected to be valid JSON, it might make sense to first try to use the built-in JSON parser, and only fall back to this library if it fails.
try { $parsed = json_decode($string, true, flags: JSON_THROW_ON_ERROR); } catch (\JsonException $e) { $parser = new \Aternos\AlmostJson\AlmostJsonParser(); $parsed = $parser->parseString($string, assoc: true); }
aternos/almost-json 适用场景与选型建议
aternos/almost-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.47k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aternos/almost-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aternos/almost-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-28