webdevcraft/jsonreader
Composer 安装命令:
composer require webdevcraft/jsonreader
包简介
JSON streaming reader (memory safety parser) with chunking option
README 文档
README
JSON streaming reader (memory safety parser) with chunking option
Purpose and Advantages
- Memory safety read of huge JSON files: solve "out of RAM" problem. Successfully tested with 1.5Gb JSON files
- Possibility to split large JSON file to smaller JSON chunks
- Custom traverse through JSON tree
- Read any JSON source: file, string, resource, character iterator
- Installation with Composer, no need any extra PHP extensions
Requirements
PHP 5.6, 7.0
Installation
With Composer:
composer require webdevcraft/jsonreader
Basic Usage
JSON Reader iterates through JSON structure elements
Each $reader->read() iterates to next element until it returns false when finished
So common usage is:
while ($reader->read()) { // next element iteration }
Reader provides information on each element:
-
DEPTH in JSON tree starting from 1:
$reader->getDepth() -
STATE (type of element):
$reader->getState(). There is syntax sugar issers exist for each of states. State could be one of:-
Object Start: JsonReaderInterface::STATE_OBJECT_START (or
$reder->isObjectStartState()) -
Object Key: JsonReaderInterface::STATE_OBJECT_KEY (or
$reder->isObjectKeyState()) -
Object End JsonReaderInterface::STATE_OBJECT_END (or
$reder->isObjectEndState()) -
Array Start JsonReaderInterface::STATE_ARRAY_START (or
$reder->isArrayStartState()) -
Array End JsonReaderInterface::STATE_ARRAY_END (or
$reder->isArrayEndState()) -
Value JsonReaderInterface::STATE_VALUE (or
$reder->isValueState())
-
-
VALUE of element by
$reader->getValue(). Types:-
For
$reader->getState() === JsonReaderInterface::STATE_VALUEthe type of value is automatically casted to PHP type: string, int, float, boolean, null -
For
JsonReaderInterface::STATE_OBJECT_KEYstate is always string -
For other states value is always null
-
Example of elements sequence for test.json is presented in JsonReaderTest.php::testReadTokens()
Chunks Usage
In case if your code is already dependent on JSON string denormanization you could use JsonReader library for chunking of huge file onto smaller JSON strings
Just start buffering chunk on element position you wish by $reader->startWriteChunk() and get chunk by $reader->finishWriteChunk(). That returnes JSON chunk string and flushes buffer.
As chunk is buffered in memory make sure your JSON schema guarantees acceptable size of chunk
That's nice practice to wrap chunks retrieve to iterator
Example of test.json chunking is presented in JsonReaderTest.php::testChunks()
Factory Usage
To create reader you have to use JsonReaderFactory
Example of JsonReader creating:
$factory = new JsonReaderFactory(); $reader = $factory->createByFilePath('/tmp/test.json');
Factory supports any sources by such methods:
- createByFilePath by file path in local filesystem
- createByString by JSON string passing
- createByResource by opened resource link, for example with fopen()
- createByCharacterTraversable - if options above do not suite your needs feel free to write your custom characters iterator using any source you have to deal with. For example you could iterate PSR-7 Message\StreamInterface::read($acceptableSize) and iterate characters inside each of read()
webdevcraft/jsonreader 适用场景与选型建议
webdevcraft/jsonreader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 10 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「stream」 「reader」 「json parser」 「memory safety」 「huge json」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webdevcraft/jsonreader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webdevcraft/jsonreader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webdevcraft/jsonreader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Object-Oriented API for PHP streams
Safely namespaced guzzle psr7 for WP2Static
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Interface for a factory to create Psr\Http\Message\StreamInterface objects
统计信息
- 总下载量: 8.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-28