suin/json

json_decode() 和 json_encode() 的简单包装器。它提供面向对象的接口和基于异常的错误处理。

1.2.0 2018-01-04 08:44 UTC

This package is auto-updated.

Last update: 2024-09-06 10:32:47 UTC


README

travis-ci-badge packagist-dt-badge license-badge release-version-badge code-climate-maintainability-badge code-climate-test-coverage-badge php-version-badge

只是一个简单的 json_decode() 和 json_encode() 包装器,但提供了更好的接口:基于异常的错误处理和面向对象的API。

功能

兼容接口

此库提供了与内置函数相同的接口,因此您可以更容易地替换您的代码。

内置函数接口

json_decode(
  string $json, 
  ?bool $assoc = false, 
  ?int $depth = 512, 
  ?int $options = 0
): mixed
json_encode(
  mixed $value, 
  ?int $options = 0,
  ?int $depth = 512 
): string

此库接口

\Suin\Json\json_decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json\json_encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string
\Suin\Json::decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json::encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string

以便开发人员可以轻松地从内置函数迁移到此库。只需在文件头部添加以下一行:

use function Suin\Json\json_decode;
use function Suin\Json\json_encode;

有关完整的迁移示例,请参阅 快速迁移

基于异常的错误处理

  • 解码 JSON 失败时抛出 DecodingException
  • 编码值失败时抛出 EncodingException
  • 您不再需要处理 json_last_error()
// Error handling example
$json = "{'Organization': 'PHP Documentation Team'}";
try {
    Json::decode($json);
} catch (Json\DecodingException $e) {
    var_dump($e->getMessage());
    var_dump($e->getContext()->json());
}
// Output:
// string(35) "Failed to decode JSON: Syntax error"
// string(42) "{'Organization': 'PHP Documentation Team'}"

面向对象接口

由于 DecoderEncoder 类可以实例化,因此您可以在多个地方重用预先配置的单个解码器/编码器。

// preconfigured decoder
$decoder = (new Decoder)->preferArray();
$array1 = $decoder->decode($json1);
$array2 = $decoder->decode($json2); // re-use it
$array3 = $decoder->decode($json3); // re-use it
  
// preconfigured encoder
$encoder = (new Encoder)->prettyPrint()->unescapeSlashes()->unescapeUnicode();
$json1 = $encoder->encode($value1);
$json2 = $encoder->encode($value2); // re-use it
$json3 = $encoder->encode($value3); // re-use it

不可变的 Decoder 对象

由于 Decoder 对象设置一旦实例化就不能更改,因此在某些模块之间共享对象的情况下,它也更加安全。

通过 Composer 安装

$ composer require suin/json

示例

使用 Json 类将 JSON 解码为值

<?php

use Suin\Json;

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(Json::decode($json));
var_dump(Json::decode($json, true));
// Output:
// object(stdClass)#%d (5) {
//   ["a"]=>
//   int(1)
//   ["b"]=>
//   int(2)
//   ["c"]=>
//   int(3)
//   ["d"]=>
//   int(4)
//   ["e"]=>
//   int(5)
// }
// array(5) {
//   ["a"]=>
//   int(1)
//   ["b"]=>
//   int(2)
//   ["c"]=>
//   int(3)
//   ["d"]=>
//   int(4)
//   ["e"]=>
//   int(5)
// }

使用 Json 类将值编码为 JSON

<?php

use Suin\Json;

$value = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
var_dump(Json::encode($value));
// Output:
// string(31) "{"a":1,"b":2,"c":3,"d":4,"e":5}"

要查看更多示例,请访问 ./example 文件夹。

更新日志

有关更多详细信息,请参阅 CHANGELOG

贡献

有关更多详细信息,请参阅 CONTRIBUTING