daverandom/exceptional-json

在失败时抛出异常的 JSON 编码和解码

v1.0.4 2018-06-11 11:25 UTC

This package is auto-updated.

Last update: 2024-08-25 07:22:12 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Packagist License

PHP 的 json_encode()json_decode() 函数的薄封装,当操作失败时抛出异常。

所需的 PHP 版本

  • PHP 7.0+

安装

$ composer require daverandom/exceptional-json

使用

像使用 json_encode()json_decode() 一样调用 \ExceptionJSON\encode()\ExceptionJSON\decode() 函数。唯一的区别是,如果操作失败,它们将抛出异常。

还定义了根命名空间中的 json_try_encode()json_try_decode(),如果它们不存在,这些只是它们命名空间对应项的别名。

$encoded = \ExceptionJSON\encode($data);
$decoded = \ExceptionJSON\decode($encoded);