germania-kg/jsondecoder

在字符串和PSR-7消息中解码JSON。支持旧版PHP中的JsonException

1.2.0 2023-01-04 08:10 UTC

This package is auto-updated.

Last update: 2024-09-19 23:40:34 UTC


README

JsonDecoder

在字符串和PSR-7消息中解码JSON。支持JsonException(注意全局命名空间!)旧版PHP中.

Packagist PHP version Tests

使用Composer安装

$ composer require germania-kg/jsondecoder

用法

可调用对象 JsonDecoder 接受PHP官方文档中json_decode 描述的相同参数。此外,它还接受一些广泛使用的数据类型

  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\StreamInterface
  • string
use Germania\JsonDecoder\JsonDecoder;

// Let:
// $response instance of ResponseInterface,
// $body instance of StreamInterface
$body = $response->getBody();
$str = $body->__toString();

try {
  $decoder = new JsonDecoder;
  $decoded = $decoder( $response );  
  $decoded = $decoder( $body );  
  $decoded = $decoder( $str );    
}
catch ( \JsonException $e)
{
  echo $e->getMessage();
}

异常

当解码失败时,将抛出一个 \JsonException(注意全局命名空间!)异常。此类由 Symfony的Polyfill PHP 7.3 提供,供缺乏PHP 7.3的用户使用。

问题

请参阅完整问题列表。

开发

$ git clone https://github.com/GermaniaKG/JsonDecoder.git
$ cd JsonDecoder
$ composer install

单元测试

可以复制 phpunit.xml.distphpunit.xml 并根据您的需要进行调整,或者保持原样。运行PhpUnit 测试或Composer脚本如下:

$ composer test
# or
$ vendor/bin/phpunit