arif-un / jcof
将 JSON 压缩减半
1.0.2
2023-09-27 10:46 UTC
Requires
- php: >=5.6
Requires (Dev)
README
更高效地表示 JSON 风格对象的方法。
关于
JCOF 力求成为 JSON 的即插即用替代品,具有大多数相同的语义,但对象表示更为紧凑。它主要通过在对象开头引入一个字符串表来实现这一点,然后使用该字符串表中的索引替换所有字符串。它还采用了一些额外的技巧,使对象尽可能小,同时不失去 JSON 最重要的好处。最重要的是,它仍然是一种基于文本的无模式格式。
示例 JSON 对象(299 字节):****
{ "people": [ {"first-name": "Bob", "age": 32, "occupation": "Plumber", "full-time": true}, {"first-name": "Alice", "age": 28, "occupation": "Programmer", "full-time": true}, {"first-name": "Bernard", "age": 36, "occupation": null, "full-time": null}, {"first-name": "El", "age": 57, "occupation": "Programmer", "full-time": false} ] }
转换后的 JCOF 对象(134 字节)降低 55%
Programmer;"age""first-name""full-time""occupation";
{"people"[(0,iw"Bob"b"Plumber")(0,is"Alice"b,s0)(0,iA"Bernard"n,n)(0,iV"El"B,s0)]}
基准测试
安装
您可以通过 Composer 安装 Slugify
composer require arif-un/jcof
用法
将 PHP 关联数组转换为字符串(json_encode)
use Jcof; $jsonString = file_get_contents(__DIR__ . '/madrid.json'); $dataAssocArray = json_decode($jsonString, true); // covert to an associative array $jcofCompressedStr = Jcof::stringify($dataAssocArray); // compressed string
转换回 JSON
use Jcof; $dataAssocArray = Jcof::parse($jcofCompressedStr); // covert jcof compressed string to associative array $jsonData = json_encode($dataAssocArray); // coverted json
其他语言实现
- JCOF JavaScript NPM
#### 此存储库是 [javascript 版本](https://github.com/mortie/jcof) 的 PHP 实现。