xp-forge/json
Composer 安装命令:
composer require xp-forge/json
包简介
Reads and writes JSON to and from various input sources
README 文档
README
Reads and writes JSON to and from various input sources.
Examples
Reading can be done from a string, file or stream:
use text\json\Json; use io\File; use peer\SocketInputStream; // Strings $value= Json::read('"Test"'); // Input $in= '{"Hello": "World"}'); $in= new File('input.json'); $in= new SocketInputStream(/* ... */); $value= Json::read($in);
Writing can be done to a string, file or stream:
use text\json\Json; use io\File; use peer\SocketOutputStream; // Strings $json= Json::of('Test'); // Output $out= new File('output.json'); $out= new SocketOuputStream(/* ... */); Json::write($value, $out);
Formatted output
To change the output format, use one of the Output implementations and pass a Format instance to the output's constructor. The formats available are:
DenseFormat($options): Best for network I/O, no unsignificant whitespace, default if nothing given and accessible viaFormat::dense($options= ~Format::ESCAPE_SLASHES).WrappedFormat($indent, $options): Wraps first-level arrays and all objects, uses whitespace after commas colons. An instance of this format using 4 spaces for indentation and per default leaving forward slashes unescaped is available viaFormat::wrapped($indent= " ", $options= ~Format::ESCAPE_SLASHES).
The available options that can be or'ed together are:
Format::ESCAPE_SLASHES: Escape forward-slashes with "" - default behavior.Format::ESCAPE_UNICODE: Escape unicode with "\uXXXX" - default behavior.Format::ESCAPE_ENTITIES: Escape XML entities&,",<and>. Per default, these are represented in their literal form.
use text\json\{FileOutput, Format}; $out= new FileOutput('glue.json', Format::wrapped()); $out->write([ 'name' => 'example/package', 'version' => '1.0.0', 'require' => [ 'xp-forge/json' => '^3.0', 'xp-framework/core' => '^10.0' ] ]);
The above code will yield the following output:
{
"name": "example/package",
"version": "1.0.0'",
"require": {
"xp-forge/json": "^3.0",
"xp-framework/core": "^10.0"
}
}
Sequential processing
Processing elements sequentially can save you memory and give a better performance in certain situations.
Reading
You can use the elements() method to receive an iterator over a JSON array. Instead of loading the entire source into memory and then returning the parsed array, it will parse one array element at a time, yielding them while going.
use peer\http\HttpConnection; use text\json\StreamInput; $conn= new HttpConnection(...); $in= new StreamInput($conn->get('/search?q=example&limit=1000')->in()); foreach ($in->elements() as $element) { // Process }
If you get a huge object, you can also process it sequentially using the pairs() method. This will parse a single key/value pair at a time.
use peer\http\HttpConnection; use text\json\StreamInput; $conn= new HttpConnection(...); $in= new StreamInput($conn->get('/resources/4711?expand=*')->in()); foreach ($in->pairs() as $key => $value) { // Process }
To detect the type of the data on the stream (again, without reading it completely), you can use the type() method.
use peer\http\HttpConnection; use text\json\StreamInput; $conn= new HttpConnection(...); $in= new StreamInput($conn->get($resource)->in()); $type= $in->type(); if ($type->isArray()) { // Handle arrays } else if ($type->isObject()) { // Handle objects } else { // Handle primitives }
Writing
To write data sequentially, you can use the begin() method and the stream it returns. This makes sense when the source offers a way to read data sequentially, if you already have the entire data in memory, using write() has the same effect.
use text\json\{StreamOutput, Types}; $query= $conn->query('select * from person'); $stream= (new StreamOutput(...))->begin(Types::$ARRAY); while ($record= $query->next()) { $stream->element($record); } $stream->close();
As the Stream class implements the Closeable interface, it can be used in the with statement:
use text\json\{StreamOutput, Types}; $query= $conn->query('select * from person'); with ((new StreamOutput(...))->begin(Types::$ARRAY), function($stream) use($query) { while ($record= $query->next()) { $stream->element($record); } });
Further reading
- Performance figures. TL;DR: While slower than the native functionality, the performance overhead is in low millisecond ranges. Using sequential processing we have an advantage both performance- and memory-wise.
- Parsing JSON is a Minefield. This library runs this test suite next to its own.
xp-forge/json 适用场景与选型建议
xp-forge/json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 225.57k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 225.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 22
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-31

