icecave/duct 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

icecave/duct

最新稳定版本:2.0.2

Composer 安装命令:

composer require icecave/duct

包简介

An incremental streaming JSON parser.

README 文档

README

Build Status Code Coverage Latest Version

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

统计信息

  • 总下载量: 3.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 77
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 76
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-05-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固