icecave/duct
Composer 安装命令:
composer require icecave/duct
包简介
An incremental streaming JSON parser.
README 文档
README
Duct is a PHP library for incrementally parsing continuous streams of JSON values.
composer require icecave/duct
Duct is designed to parse sequential JSON values from data streams, without framing or demarcation outside of the JSON specification.
Examples
Simple parsing
Duct can be used to parse multiple JSON documents in a single call to Parser::parse().
The JSON string given must contain complete values.
use Icecave\Duct\Parser; $parser = new Parser; $values = $parser->parse('[ 1, 2, 3 ] [ 4, 5, 6 ]'); assert($values[0] === [1, 2, 3]); assert($values[1] === [4, 5, 6]);
Incremental parsing
Asynchronous, incremental parsing is also possible using the Parser::feed(), values() and finalize() methods.
use Icecave\Duct\Parser; $parser = new Parser; // JSON data can be fed to the parser incrementally. $parser->feed('[ 1, '); // An array of completed values can be retreived using the values() method. // At this point no complete object has been parsed so the array is empty. $values = $parser->values(); assert(0 === count($values)); // As more data is fed to the parser, we now have one value available, an array // of elements 1, 2, 3. $parser->feed('2, 3 ][ 4, 5'); $values = $parser->values(); assert(1 === count($values)); assert($values[0] == [1, 2, 3]); // Note that calling values() is destructive, in that any complete objects are // removed from the parser and will not be returned by future calls to values(). $values = $parser->values(); assert(0 === count($values)); // Finally we feed the remaining part of the second object to the parser and the // second value becomes available. $parser->feed(', 6 ]'); $values = $parser->values(); assert(1 === count($values)); assert($values[0] == [4, 5, 6]); // At the end of the JSON stream, finalize is called to parse any data remaining // in the buffer. An exception is thrown if the buffer contains an incomplete // value. $parser->finalize(); // In this case there were no additional values. $values = $parser->values(); assert(0 === count($values));
Event-based parsing
Duct also provides EventedParser, an event-based incremental parser similar to the Clarinet
library for JavaScript. Event management is provided by Événement, a popular PHP
event library.
As per the example above the feed() and finalize() methods are used, however there is no values() method. Instead,
the following events are emitted as the buffer is parsed.
- document-open: emitted when a JSON document is begun
- document-close: emitted after an entire JSON document has been parsed
- array-open: emitted when an array open bracket is encountered
- array-close: emitted when an array closing bracket is encountered
- object-open: emitted when an object open brace is encountered
- object-close: emitted when an object closing brace is encountered
- object-key (string $key): emitted when an object key is encountered
- value (mixed $value): emitted whenever a scalar or null is encountered, including inside objects and arrays
- error (Exception $error): emitted when a syntax error is encountered
icecave/duct 适用场景与选型建议
icecave/duct 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.52k 次下载、GitHub Stars 达 76, 最近一次更新时间为 2013 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「event」 「stream」 「parser」 「parse」 「react」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 icecave/duct 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 icecave/duct 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 icecave/duct 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 3.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 77
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-05-02