aalfiann/json-class-php
Composer 安装命令:
composer require aalfiann/json-class-php
包简介
A class for handle json in a better way.
README 文档
README
A class for handle json in a better way.
Installation
Install this package via Composer.
composer require "aalfiann/json-class-php:^1.0"
Usage
Encode
require_once ('vendor/autoload.php'); use \aalfiann\JSON; $data = [ 'result'=>'just make a test!', 'data' => [ 'id' => '1', 'user' => 'your name', 'email' => 'your_email@gmail.com', 'website' => 'http://yourdomain.com', 'non-utf8' => 'السلام علیکم ورحمة الله وبرکاته!' ] ]; header('Content-Type: application/json'); $json = new JSON; echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
Decode
require_once ('vendor/autoload.php'); use \aalfiann\JSON; $data = '{"result":"just make a test!","data":{"id":"1","user":"your name","email":"your_email@gmail.com","website":"http://yourdomain.com","non-utf8":"\u00d8\u00a7\u00d9\u0084\u00d8\u00b3\u00d9\u0084\u00d8\u00a7\u00d9\u0085 \u00d8\u00b9\u00d9\u0084\u00db\u008c\u00da\u00a9\u00d9\u0085 \u00d9\u0088\u00d8\u00b1\u00d8\u00ad\u00d9\u0085\u00d8\u00a9 \u00d8\u00a7\u00d9\u0084\u00d9\u0084\u00d9\u0087 \u00d9\u0088\u00d8\u00a8\u00d8\u00b1\u00da\u00a9\u00d8\u00a7\u00d8\u00aa\u00d9\u0087!"},"logger":{"timestamp":"2018-09-17 13:53:12","uniqid":"5b9f95a812c0f"}}'; $json = new JSON; echo var_dump($json->decode($data,true));
Debug
You can easily debug with set property debug to true.
require_once ('vendor/autoload.php'); use \aalfiann\JSON; header('Content-Type: application/json'); $json = new JSON; $json->debug=true; echo $json->encode($data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
Chain Usage Example
require_once ('vendor/autoload.php'); use \aalfiann\JSON; header('Content-Type: application/json'); $json = new JSON; echo $json->withSanitizer()->withLog()->encode(['user'=>'yourname'],JSON_PRETTY_PRINT);
Properties
$withlog=false,$sanitize=false,$ansii=false,$debug=false,$makesimple=false,$trim=false;
Chain Function
- withSanitizer($sanitize=true) this will sanitize your data array before execute json_encode.
- withTrim($trim=true) this will trimmed your data array or string before execute json_encode or json_decode.
- withLog($withlog=true) this will append the logger data into your json.
- setAnsii($ansii=true) this will make sanitizer works to handle ANSII chars.
- setDebug($debug=true) this is for debugging purpose.
- makeSimple($makesimple=true) this will hide the additional data in debug output.
Main Function
- encode($data,$options=0,$depth=512) encode array or string to json format.
- decode($json,$assoc=false,$depth=512,$options=0) decode json string to
stdClass/array. - isValid($json=null) to determine json string is valid or not.
Helper Function
- convertToUTF8($string) convert string to valid UTF8 chars.
- convertToUTF8Ansii($string) convert string to valid UTF8 chars (support ANSII chars).
- trimValue($string) trim the data array or string to strip the whitespace or any characters consider to blank value.
- debug_encode($string,$options=0,$depth=512) debugger to test json encode.
- debug_decode($json,$assoc=false,$depth=512,$options=0) debugger to test json decode.
- errorMessage($jsonlasterror,$content) case error message about json.
- fixControlChar($string) Most common fixed hidden control char in json string which made json decode fails.
- modifyJsonStringInArray($data,$jsonfield,$setnewfield="") modify json data string in some field array to be nice json data structure.
- concatenate($data,$escape=true,$options=0,$depth=512) concatenate json data
- minify($json) Minify the json string
aalfiann/json-class-php 适用场景与选型建议
aalfiann/json-class-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「class」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aalfiann/json-class-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aalfiann/json-class-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aalfiann/json-class-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Collection of methods that I or you usually use.
A very simple yet useful helper class to handle PHP file uploads.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Yii2 js base class
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-17