eftec/services_json
Composer 安装命令:
composer require eftec/services_json
包简介
PHP implementaion of json_encode/decode
README 文档
README
PHP implementation of json_encode/decode for PHP 7.2 and higher. This library works with and without quoted keys.
This package provides a simple encoder and decoder for JSON notation. It is intended for use with client-side Javascript applications that make use of HTTPRequest to perform server communication functions - data can be encoded into JSON notation for use in a client-side javascript, or decoded from incoming Javascript requests. JSON format is native to Javascript, and can be directly eval() with no further parsing overhead.
So, what is the goal with this version?
While this version doesn't have a better performance than json_encode() and json_decode() available as extension, but it has the next features:
- it doesn't require an extension. If you can't install ext-json, then you can use this version.
- it works with JSON with unquoted keys (for example JavaScript notation), {aaa:2,bbb:"hello"}
- it could also work with unquoted values {"aaa":2,"bbb":hello}
- it could also work with unwrapped content "aaa":2,"bbb":"hello" instead of {aaa:2,bbb:"hello"}
- It is a simple .php file with no dependency.
Usage
Getting started using composer
- Install the library using composer
composer require eftec/services_json
- Include the dependency
use eftec\ServicesJson\Services_JSON; include '../vendor/autoload.php';
See the folder examples for further examples
Getting started without composer
-
Copy this file: Services_JSON/Services_JSON.php
-
Include the file
use eftec\ServicesJson\Services_JSON; include 'Services_JSON.php'; // or where is located the file.
Decode
Decode a JSON string into a stdclass or an associative array
$json='{"hello":{"a":2,"b":3},"world":[1,2,3,"aaa"]}'; var_dump(Services_JSON::decode($json)); // as stdclass var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
It also works with unquoted keys
$json='{hello:{a:2,b:3},world:[1,2,3,"aaa","bbbb"]}'; // the keys are unquoted. var_dump(Services_JSON::decode($json)); // as stdclass var_dump(Services_JSON::decode($json,Services_JSON::GET_ARRAY)); // as array
Json unwrapped.
With the flag Services_JSON::DECODE_FIX_ROOT, it also works when the origin text misses [] and {} at the start of the code. Note, this feature is not foolproof, for example "[1,2,3],[4,5,6]" will not wrap as "[[1,2,3],[4,5,6]]"
{"a":222,"b:"ccc"} # normal json
"a":222,"b:"ccc" # json without the root parenthesis.
Services_JSON::decode('1,2,3',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT); // returns [1,2,3] Services_JSON::decode('"k1":"v1", k2:2',Services_JSON::GET_ARRAY | Services_JSON::DECODE_FIX_ROOT) // returns [ 'k1' => 'v1','k2'=>2]
Note: DECODE_FIX_ROOT flag detects if the near character is ":" or ",". If the closest character is ":", then it returns an object, otherwise it returns a list. If there is none, then it returns a list.
Json with unquoted values
With the flag Services_JSON::DECODE_NO_QUOTE it also works when the string values are not quoted.
Note: invisible characters at the beginner and at the end (tabs, line carriages) will be trimmed.
Services_JSON::decode('{"a":aaa,"b":bbbb,"c": aaaaa}' , Services_JSON::GET_ARRAY | Services_JSON::DECODE_NO_QUOTE) // ['a'=>'aaa','b'=>'bbbb','c' => 'aaaaa']
Encode
Encode transform a value (array, object, primitive value, etc.) into a json expression (a string)
$array=["hello"=>['a'=>2,'b'=>3],'world'=>[1,2,3,"aaa","bbb"]]; $obj=(object)$array; var_dump(Services_JSON::encode($array)); // encode an associative array var_dump(Services_JSON::encode($obj)); // encode an object
Changelog
- 2.5
- now allows "@" and "." for key values.
- 2.4
- update requirements to php 7.4
- added more type hinting (type validation)
- 2.3.2
- Added flag to decode() DECODE_NO_QUOTE
- 2.3.1
- deleted unused code
- fixed comments.
- 25% of the code has been refactored.
- 2.3
- Fixed a typo with a comment.
- added phpunit. The entire code is tested but special codification.
- 2.2
- Now the library is static, so you can call the methods without creating an instance.
- If you want to work with the non-static library, then install 1.1
- 1.1
- It works with PHP 7.2 and higher (including PHP 8.0 and 8.1)
- It doesn't require PECL to work.
- The code was cleaned
- Web header is removed.
- Method decode() could return an associative array.
- 1.0.3-1.0.0 PECL version. It only works with PHP 4.x and PHP 5.x
eftec/services_json 适用场景与选型建议
eftec/services_json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.14k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 eftec/services_json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eftec/services_json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 31.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2022-11-07