8ctopus/relaxed-json

厌倦了JSON严格的语法吗?那么relaxed JSON正适合你。

1.0.2 2023-12-28 11:18 UTC

This package is auto-updated.

Last update: 2024-08-26 14:39:26 UTC


README

packagist downloads min php version license tests code coverage badge lines of code

厌倦了JSON严格的语法吗?那么relaxed JSON正适合你。

特性

  • 支持所有形式的注释 // 我的注释, /* 我的注释 */
  • 忽略数组或对象末尾的逗号
  • 当JSON无效时抛出异常
  • 零依赖

安装

composer require 8ctopus/relaxed-json

使用

use Oct8pus\RelaxedJson;

$json = <<<JSON
{
    // maximum requests per hour
    "throttleThreshold": 300,
}

JSON;

var_dump(RelaxedJson::decode($json, true));
array(1) {
  'name' =>
  string(20) "8ctopus/relaxed-json"
}

异常

$json = <<<JSON
{
    "throttleThreshold" => 300,
}

JSON;

// throws RelaxedJsonException Syntax error
RelaxedJson::decode($json, true);

致谢

此库是对来自https://github.com/etconsilium/php-json-fix的原始工作的改进重新打包

测试

composer test

整洁的代码

composer fix
composer fix-risky