umulmrum/json-parser
Composer 安装命令:
composer require umulmrum/json-parser
包简介
A library that parses JSON as stream.
README 文档
README
JSON Parser is a streaming JSON parsing library. It returns the same results as
\json_decode('some-string', true, 512, JSON_UNESCAPED_SLASHES).
You might want to use this library when decoding JSON that is multiple MiB in size,
where the in-memory approach of \json_decode() consumes excessive amounts of memory.
This library has constant memory usage independent of the size of the JSON string if
used correctly (which is not difficult, see below).
You might NOT want to use this library for small JSON strings of less than ~1 MiB, as
\json_decode() is about 50x faster. While I aim for performance improvements, the C
implementation of \json_decode() will always be faster than this PHP implementation.
Requirements
- PHP >= 7.1
That's it really. However, using the mb_string extension will improve performance.
Installation
Install the library using Composer.
composer require umulmrum/json-parser
Status
This library is in beta state. Code coverage in unit tests is high and the library is already used in production, but it is not yet considered stable; you are encouraged to test it and report issues.
Usage
<?php include __DIR__.'/vendor/autoload.php'; $parser = umulmrum\JsonParser\JsonParser::fromFile('/path/to/file.json'); foreach ($parser->generate() as $value) { var_dump($value); }
Every valid JSON string consists of either a root array or a root object containing values.
The generate() method returns these first-level values, one at a time.
Each returned value is a PHP array containing a single key/value pair; the key is
- the key of the first-level value if the root element is an object.
- the index of the first-level value if the root element is an array.
These values can then be consumed (e.g. persisted, printed or sent by AMQP) and be discarded afterwards.
If the JSON string consists of an empty object or array, an empty array is returned.
If the JSON string is empty or consists only of whitespace, null is returned.
On errors, one of these exceptions is thrown:
\Umulmrum\JsonParser\InvalidJsonExceptionif invalid JSON is encountered. The exception provides information on the line and column of the JSON string in which the error occurred (sometimes this information is off by one line or column).\Umulmrum\JsonParser\DataSource\DataSourceExceptionif the source file could not be read for any reason.
If no exception was thrown, the result can be "trusted" (i.e. there is no need to check for errors afterwards
such as \json_last_error()).
Alternative Usage
Alternatively, instantiate the parser from a JSON string:
$parser = umulmrum\JsonParser\JsonParser::fromString('["test"]');
Be aware that this will not be very memory-efficient as PHP uses a lot of memory for strings. Whenever
possible, the fromFile() factory method is recommended.
JsonParser can also return the complete content by calling all() (which is again not recommended
because of memory).
Customization
Internally, JsonParser uses an implementation of \Umulmrum\JsonParser\DataSource\DataSourceInterface to receive
the characters to parse. The factory methods for JsonParser are simple convenience methods that handle instantiating
the data source, but the constructor can be called manually, passing a data source, so that custom data
source implementations can be used. See the docblocks of \Umulmrum\JsonParser\DataSource\DataSourceInterface for
details. You might wish to extend \Umulmrum\JsonParser\DataSource\AbstractDataSource that contains some convenience
methods.
The example from the previous section can be written as follows:
$parser = new Umulmrum\JsonParser\JsonParser(new \Umulmrum\JsonParser\DataSource\FileDataSource('["test"]'));
Backwards Compatibility
This library follows Semantic Versioning and will therefore only introduce breaking changes in the public API in major versions (and in minor versions before reaching 1.0.0). The public API consists of all classes, except
- everything marked as @internal
- tests
Contribution
Contributions are highly welcome. Please follow these rules when submitting a PR:
- Mimic existing code for style and structure.
- Add unit tests for all of your code.
- Use the Symfony code style (php-cs-fixer with the
.php_cs.distruleset in this repository).
By submitting a PR you agree that all your contributed code may be used under the MIT license.
License
This library is licensed under the MIT License. See LICENSE for details.
umulmrum/json-parser 适用场景与选型建议
umulmrum/json-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「stream」 「parser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 umulmrum/json-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 umulmrum/json-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 umulmrum/json-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
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
统计信息
- 总下载量: 124
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-15