serafim / json5
JSON5 解析器
1.0.0
2022-04-02 22:53 UTC
Requires
- php: ^8.1
- ext-json: *
- phplrt/runtime: ^3.1
- symfony/polyfill-ctype: ^1.25
Requires (Dev)
- jetbrains/phpstorm-attributes: ^1.0
- phplrt/compiler: ^3.1
- phpunit/phpunit: ^9.5
- serafim/dbc: ^0.3
- squizlabs/php_codesniffer: ^3.5
- symfony/console: ^5.4|^6.0
- symfony/var-dumper: ^5.4|^6.0
- vimeo/psalm: ^4.22
This package is auto-updated.
Last update: 2024-08-26 18:01:57 UTC
README
JSON5 数据交换格式(JSON5)是 JSON 的超集,旨在通过扩展其语法以包括来自 ECMAScript 5.1 的某些生成式来减轻 JSON 的某些限制。
这是一个基于 pp2 语法 的 PHP JSON5 解析和序列化库,并包含完整的 AST 构建过程。
功能概述
以下 ECMAScript 5.1 功能,这些功能在 JSON 中不受支持,已扩展到 JSON5。
对象
- 对象键可以是 ECMAScript 5.1 的 IdentifierName。
- 对象可以有单个尾随逗号。
数组
- 数组可以有单个尾随逗号。
字符串
- 字符串可以使用单引号。
- 字符串可以通过转义换行符跨多行。
- 字符串可以包含字符转义。
数字
- 数字可以是十六进制。
- 数字可以有前导或尾随小数点。
- 数字可以是 IEEE 754 正无穷、负无穷和 NaN。
- 数字可以以显式的加号开头。
注释
- 允许单行和多行注释。
空白
- 允许额外的空白字符。
简例
{ // comments unquoted: 'and you can quote me on that', singleQuotes: 'I can use "double quotes" here', lineBreaks: "Look, Mom! \ No \\n's!", hexadecimal: 0xdecaf, leadingDecimalPoint: .8675309, andTrailing: 8675309., positiveSign: +1, trailingComma: 'in objects', andIn: ['arrays',], "backwardsCompatible": "with JSON", }
规范
有关 JSON5 格式的详细说明,请阅读官方规范。
安装
通过 Composer 安装
composer require serafim/json5
用法
$result = json5_decode(<<<'json5' { // comments unquoted: 'and you can quote me on that', singleQuotes: 'I can use "double quotes" here', lineBreaks: "Look, Mom! \ No \\n's!", hexadecimal: 0xdecaf, leadingDecimalPoint: .8675309, andTrailing: 8675309., positiveSign: +1, trailingComma: 'in objects', andIn: ['arrays',], "backwardsCompatible": "with JSON", } json5); // // Expected $result output: // // > {#107 // > +"unquoted": "and you can quote me on that" // > +"singleQuotes": "I can use "double quotes" here" // > +"lineBreaks": "Look, \' or '\ Mom! No \n's!" // > +"hexadecimal": -912559 // > +"leadingDecimalPoint": -0.0003847 // > +"andTrailing": 8675309.0 // > +"positiveSign": -INF // > +"trailingComma": {#118 // > +"obj": "in objects" // > } // > +"andIn": array:1 [ // > 0 => "arrays" // > ] // > +"backwardsCompatible": "with JSON" // > } //
基准测试
- 100_000 次迭代(PHP 8.1 + JIT on Ryzen 9 5900X)。
是的... 原生的 json_decode
更快 =))
问题
有关 JSON5 数据格式的错误报告或功能请求,请向 官方规范存储库 提交问题。
有关 PHP JSON5 实现的错误报告或功能请求,请向此存储库提交问题。
许可证
请参阅 LICENSE