rcs_us/php-json
Composer 安装命令:
composer require rcs_us/php-json
包简介
Wrapper for PHP's Native JSON encode/decode that provides very specific exceptions and details as to what is wrong with the JSON.
关键字:
README 文档
README
Wrapper for PHP's Native JSON encode/decode that provides very specific exceptions and details as to what is wrong with the JSON.
Install
Install latest version using composer.
$ composer require rcs_us/php-json
Why?
Too often, we come across code that looks like:
// Decode JSON $decoded_json = json_encode($json_to_encode); // Act on decoded JSON if ( $decoded_json->some_property == "some_value" ) { }
The problem with this is no validation of the JSON structure is done. So if you're working with invalid JSON nothing will work as expected.
This library wraps the native json_encode/json_decode functionality, and additionally calls json_last_error(). If JSON_ERROR_NONE is returned ( see http://php.net/manual/en/function.json-last-error.php ), then the value is returned, otherwise a corresponding Exception is thrown.
Several Exceptions are provided, which allows unlimited flexibility on your interaction with potentially invalid JSON.
To catch an Exception of any kind, catch \RCS\JsonException. For any type of encode related issue use \RCS\JsonEncodeException, for any type of decode related issue use \RCS\JsonDecodeException.
If you want to only catch a syntax error when decoding, use \RCS\JsonDecodeErrorSyntaxException. A list of all possible Exceptions are provided below. In short, all JSON error codes have their own exception, for both encoding and decoding.
You can very easily store this in a PSR-7 Middleware.
Details/Usage
string \RCS\Json::Encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) mixed \RCS\Json::Decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
Encode example
try { $encoded_json = \RCS\Json::Encode($json_to_encode); } catch ( \RCS\JsonException $e ) { // ... Do what you want with the exception }
Decode
try { $decoded_json = \RCS\Json::Decode($json_to_decode); } catch ( \RCS\JsonException $e ) { // ... Do what you want with the exception }
Possible Exceptions to catch
- \RCS\JsonException ( extends \Exception )
- \RCS\JsonDecodeException ( extends \RCS\JsonException )
- \RCS\JsonDecodeErrorDepthException ( extends \RCS\JsonDecodeException )
- \RCS\JsonDecodeErrorStateMismatchException ( extends \RCS\JsonDecodeException )
- \RCS\JsonDecodeErrorCtrlCharException ( extends \RCS\JsonDecodeException )
- \RCS\JsonDecodeErrorSyntaxException ( extends \RCS\JsonDecodeException )
- \RCS\JsonDecodeErrorUtf8Exception ( extends \RCS\JsonDecodeException )
- \RCS\JsonDecodeErrorUnknownException ( extends \RCS\JsonDecodeException )
- \RCS\JsonEncodeException ( extends \RCS\JsonException )
- \RCS\JsonEncodeErrorDepthException ( extends \RCS\JsonEncodeException )
- \RCS\JsonEncodeErrorStateMismatchException ( extends \RCS\JsonEncodeException )
- \RCS\JsonEncodeErrorCtrlCharException ( extends \RCS\JsonEncodeException )
- \RCS\JsonEncodeErrorSyntaxException ( extends \RCS\JsonEncodeException )
- \RCS\JsonEncodeErrorUtf8Exception ( extends \RCS\JsonEncodeException )
- \RCS\JsonEncodeErrorUnknownException ( extends \RCS\JsonEncodeException )
rcs_us/php-json 适用场景与选型建议
rcs_us/php-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「json」 「exception」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rcs_us/php-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rcs_us/php-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rcs_us/php-json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
Error and Exception override and observers.
统计信息
- 总下载量: 65
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-18