pnz / json-exception
带有错误时抛出异常的Json解码/编码库。
v1.0.1
2021-08-18 13:32 UTC
Requires
- php: ^7.0 || ^8.0
- ext-json: *
This package is auto-updated.
Last update: 2024-09-18 20:44:02 UTC
README
用法
此库提供了 Json::decode() 和 Json::encode() 实现功能,在错误时抛出 \JsonException。
要使用此库
- 使用composer安装库:
composer require pnz/json-exception - 将
json_decode()的使用替换为Json::decode() - 将
json_encode()的使用替换为Json::encode() - 最终捕获函数抛出的
\JsonException
示例:旧代码
$data = json_decode($jsonString, ...); if (ERROR_NONE !== json_last_error()) { // handle the error: thown a custom exception, or return // $error = json_last_error_msg(); // $errorCode = json_last_error(); }
新代码
try { $data = Json::decode($jsonString, ...); } catch(\JsonException $e) { // Handle the exception }
开发
要运行PHP编码风格检查(php-cs-fixer 和 phpstan),运行 make phpcs 命令来
- 在
tools/中下载php-cs-fixer工具(如果尚未存在) - 在
tools/中下载phpstan工具(如果尚未存在) - 在源代码上运行
php-cs-fixer - 在源代码上运行
phpstan
要运行测试,使用 make tests 来
- 如果不存在,在
tools/中下载phpunit工具 - 运行
phpunit测试