inkrot/php-compress-json
Composer 安装命令:
composer require inkrot/php-compress-json
包简介
Store JSON data in space efficient manner
README 文档
README
A PHP library to compress large JSON documents with repeated structures and values. Helps to reduce the size of a JSON file, for example, during storage or transmission over the network, while preserving the document structure and key names.
Inspired by compress-json. And thanks to @beenotung.
This library is fully compatible with compress-json for NodeJS.
Features
- Supports all JSON types
- Object key order is preserved
- Repeated values are stored only once
- Numbers are encoded in base62 format (0-9A-Za-z)
- Custom Memory providers for store and cache during compress heavy data (see class AbstractMemory).
You can pass your custom memory provider through the
Compressor::createWithMemory()method - Passing custom
json_encodeorjson_decodearguments intoJsonordecompressJsonmethods respectively
Special Values
The following special values are supported (v3.2.0 compatibility):
| Value | Encoded |
|---|---|
null |
'' (empty string) |
true |
b|T |
false |
b|F |
INF (Infinity) |
N|+ |
-INF (-Infinity) |
N|- |
NAN (Not a Number) |
N|0 |
Example:
use CompressJson\Core\Compressor; $data = [ 'infinity' => INF, 'negInfinity' => -INF, 'nan' => NAN, ]; $compressedJson = Compressor::create() ->compress($data) ->toJson(); // Decompress $decompressed = Compressor::create() ->decompressJson($compressedJson); var_dump($decompressed['infinity']); // float(INF) var_dump($decompressed['negInfinity']); // float(-INF) var_dump(is_nan($decompressed['nan'])); // bool(true)
Install
Via Composer
composer require inkrot/php-compress-json
Usage
Compress
use CompressJson\Core\Compressor; $data = [ 'key1' => 'value1', 'key2' => 'value3', 'key3' => [ 'nestedKey1' => 'value1' ] ]; $compressedJson = Compressor::create() ->compress($data) ->toJson(); print_r($compressedJson); // encoded string in JSON format
Output:
[["key1","key2","key3","a|0|1|2","value1","value3","nestedKey1","a|6","o|7|4","o|3|4|5|8"],"9"]
Decompress
use CompressJson\Core\Compressor; $compressedJson = '[["key1","key2","key3","a|0|1|2","value1","value3","nestedKey1","a|6","o|7|4","o|3|4|5|8"],"9"]'; $data = Compressor::create() ->decompressJson($compressedJson); print_r($data); // array
Output:
Array
(
[key1] => value1
[key2] => value3
[key3] => Array
(
[nestedKey1] => value1
)
)
Example structure for efficient compression
use CompressJson\Core\Compressor; $data = [ 'count' => 3, 'names' => ['New York', 'London', 'Paris', 'Beijing', 'Moscow'], 'cities' => [ [ 'id' => 1, 'name' => 'New York', 'countryName' => 'USA', 'location' => [ 'latitude' => 40.714606, 'longitude' => -74.002800, ], 'localityType' => 'BIG_CITY', ], [ 'id' => 2, 'name' => 'London', 'countryName' => 'UK', 'location' => [ 'latitude' => 51.507351, 'longitude' => -0.127696, ], 'localityType' => 'COUNTRY_CAPITAL', ], [ 'id' => 3, 'name' => 'Paris', 'countryName' => 'France', 'location' => [ 'latitude' => 48.856663, 'longitude' => 2.351556, ], 'localityType' => 'COUNTRY_CAPITAL', ], [ 'id' => 4, 'name' => 'Beijing', 'countryName' => 'China', 'location' => [ 'latitude' => 39.901850, 'longitude' => 116.391441, ], 'localityType' => 'COUNTRY_CAPITAL', ], [ 'id' => 5, 'name' => 'Moscow', 'countryName' => 'Russia', 'location' => [ 'latitude' => 55.755864, 'longitude' => 37.617698, ], 'localityType' => 'COUNTRY_CAPITAL', ], ] ]; $compressedJson = Compressor::create() ->compress($data) ->toJson(); print_r(json_encode($data)); echo PHP_EOL; print_r($compressedJson);
Pure JSON (749 chars)
{"count":3,"names":["New York","London","Paris","Beijing","Moscow"],"cities":[{"id":1,"name":"New York","countryName":"USA","location":{"latitude":40.714606,"longitude"
:-74.0028},"localityType":"BIG_CITY"},{"id":2,"name":"London","countryName":"UK","location":{"latitude":51.507351,"longitude":-0.127696},"localityType":"COUNTRY_CAPITAL
"},{"id":3,"name":"Paris","countryName":"France","location":{"latitude":48.856663,"longitude":2.351556},"localityType":"COUNTRY_CAPITAL"},{"id":4,"name":"Beijing","coun
tryName":"China","location":{"latitude":39.90185,"longitude":116.391441},"localityType":"COUNTRY_CAPITAL"},{"id":5,"name":"Moscow","countryName":"Russia","location":{"l
atitude":55.755864,"longitude":37.617698},"localityType":"COUNTRY_CAPITAL"}]}
Compressed JSON (562 chars)
[["count","names","cities","a|0|1|2","n|3","New York","London","Paris","Beijing","Moscow","a|5|6|7|8|9","id","name","countryName","location","localityType","a|B|C|D|E|F
","n|1","USA","latitude","longitude","a|J|K","n|e.2Xkv","n|-1C.28G","o|L|M|N","BIG_CITY","o|G|H|5|I|O|P","n|2","UK","n|p.dz7","n|-0.2vFR","o|L|T|U","COUNTRY_CAPITAL","o
|G|R|6|S|V|W","France","n|m.1XNq","n|2.2kQz","o|L|Z|a","o|G|4|7|Y|b|W","n|4","China","n|d.F7F","n|1s.bVh","o|L|f|g","o|G|d|8|e|h|W","n|5","Russia","n|t.1xtN","n|b.3lHA"
,"o|L|l|m","o|G|j|9|k|n|W","a|Q|X|c|i|o","o|3|4|A|p"],"q"]
In this example, compression gives an efficiency of 25%. However, the more complex and repetitive the structure, the greater the compression efficiency.
Testing
composer test
Credits
License
BSD 2-Clause License (Free Open Sourced Software)
inkrot/php-compress-json 适用场景与选型建议
inkrot/php-compress-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 82.53k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「compress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 inkrot/php-compress-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inkrot/php-compress-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 inkrot/php-compress-json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
Kinikit - PHP Application development framework MVC component
OriginPHP Html
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
PHP client for the Google Closure Compiler API in one file.
统计信息
- 总下载量: 82.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 15
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2023-03-20