suin/json 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

suin/json

Composer 安装命令:

composer require suin/json

包简介

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

README 文档

README

travis-ci-badge packagist-dt-badge license-badge release-version-badge code-climate-maintainability-badge code-climate-test-coverage-badge php-version-badge

Just a simple wrapper of json_decode() and json_encode(), but provides better interfaces: exception-based error handling and object oriented APIs.

Features

Compatible interface

This library provides the same interface with built-in functions, so you can replace your code easier.

Built-in function interface:

json_decode(
  string $json, 
  ?bool $assoc = false, 
  ?int $depth = 512, 
  ?int $options = 0
): mixed
json_encode(
  mixed $value, 
  ?int $options = 0,
  ?int $depth = 512 
): string

This library interface:

\Suin\Json\json_decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json\json_encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string
\Suin\Json::decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json::encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string

So that developers easily migrate to this library from the built-in functions. Just adding the following one line in the head of file:

use function Suin\Json\json_decode;
use function Suin\Json\json_encode;

For about the full migration example, see quick migration.

Exception-based error handling

  • Throws DecodingException when failed to decode JSON.
  • Throws EncodingException when failed to encode values.
  • You don't have to treat json_last_error() any more.
// Error handling example
$json = "{'Organization': 'PHP Documentation Team'}";
try {
    Json::decode($json);
} catch (Json\DecodingException $e) {
    var_dump($e->getMessage());
    var_dump($e->getContext()->json());
}
// Output:
// string(35) "Failed to decode JSON: Syntax error"
// string(42) "{'Organization': 'PHP Documentation Team'}"

Object-oriented interface

As Decoder and Encoder class can be instantiated, you can re-use a preconfigured single decoder/encoder in several places.

// preconfigured decoder
$decoder = (new Decoder)->preferArray();
$array1 = $decoder->decode($json1);
$array2 = $decoder->decode($json2); // re-use it
$array3 = $decoder->decode($json3); // re-use it
  
// preconfigured encoder
$encoder = (new Encoder)->prettyPrint()->unescapeSlashes()->unescapeUnicode();
$json1 = $encoder->encode($value1);
$json2 = $encoder->encode($value2); // re-use it
$json3 = $encoder->encode($value3); // re-use it

Immutable Decoder object

As the Decoder object setting can not be changed once being instantiated, it is safer even in the case of sharing the object among some modules.

Installation via Composer

$ composer require suin/json

Example

Decoding JSON to values using Json class

<?php

use Suin\Json;

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(Json::decode($json));
var_dump(Json::decode($json, true));
// Output:
// object(stdClass)#%d (5) {
//   ["a"]=>
//   int(1)
//   ["b"]=>
//   int(2)
//   ["c"]=>
//   int(3)
//   ["d"]=>
//   int(4)
//   ["e"]=>
//   int(5)
// }
// array(5) {
//   ["a"]=>
//   int(1)
//   ["b"]=>
//   int(2)
//   ["c"]=>
//   int(3)
//   ["d"]=>
//   int(4)
//   ["e"]=>
//   int(5)
// }

Encoding values to JSON using Json class

<?php

use Suin\Json;

$value = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
var_dump(Json::encode($value));
// Output:
// string(31) "{"a":1,"b":2,"c":3,"d":4,"e":5}"

To see more examples, visit ./example folder.

Changelog

Please see CHANGELOG for more details.

Contributing

Please see CONTRIBUTING for more details.

suin/json 适用场景与选型建议

suin/json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.59k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「encoder」 「exceptions」 「wrapper」 「decoder」 「json_encode」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 suin/json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 suin/json 我们能提供哪些服务?
定制开发 / 二次开发

基于 suin/json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 23
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-29