pnz/json-exception

带有错误时抛出异常的Json解码/编码库。

v1.0.1 2021-08-18 13:32 UTC

This package is auto-updated.

Last update: 2024-09-18 20:44:02 UTC


README

Latest Version Downloads Software License Build Status

用法

此库提供了 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-fixerphpstan),运行 make phpcs 命令来

  • tools/ 中下载 php-cs-fixer 工具(如果尚未存在)
  • tools/ 中下载 phpstan 工具(如果尚未存在)
  • 在源代码上运行 php-cs-fixer
  • 在源代码上运行 phpstan

要运行测试,使用 make tests

  • 如果不存在,在 tools/ 中下载 phpunit 工具
  • 运行 phpunit 测试