crocodile2u/json-streamer 问题修复 & 功能扩展

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

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

crocodile2u/json-streamer

最新稳定版本:v1.0.0

Composer 安装命令:

composer require crocodile2u/json-streamer

包简介

Memory-efficient and fast streaming JSON parser

README 文档

README

Memory-efficient and fast streaming JSON parser.

Example: parsing GeoJson from a gzipped JSON file containing a FeatureCollection, streaming it feature by feature. The second parameter to JsonStreamer constructor is the path in your JSON structure that contains the array that you want streamed. In this example, GeoJson's FeatureCollection is an object that has a 'features' property with an array of feature objects.

$fp = fopen("compress.zlib://" . __DIR__ . "/Fiji.GeoJson.gz", "r");
$parser = new \JsonStreamer\JsonStreamer($fp, "features");
foreach ($parser as $val) {
    echo substr(json_encode($val), 0, 100)."\n\n";
}

The array that you'd like to stream can contained deeply nested in the JSON structure. Here, the data that we're interested in, are located at level 3. If you were to access this array in Javascript, that would be like this:

let container = {
    "lvl1": {
        "lvl2": {
            "lvl3": [
                [1, 2, 3],
                [1, 2, 3, 4],
                [1, 2, 3, 4, 5],
                [1, 2, 3, 4, 5, 6]
            ]
        }
    }
}
let data = container.lvl1.lvl2.lvl3

To iterate through the same data in PHP with JsonStreamer, do the following:

$fp = fopen('data://text/plain,' . $json,'r');
$parser = new \JsonStreamer\JsonStreamer($fp, "lvl1.lvl2.lvl3");
foreach ($parser as $val) {
    echo json_encode($val) . "\n\n";
}

JsonStreamer has been built with GeoJson in mind, and is only capable of iterating over JSON arrays (possibly nested inside wrapper objects). However, it does it's work quite efficiently, as compared to other streaming JSON parsers. I was able to parse huge GeoJson files containing enormous GeoJson shapes (multipolygons) tens times faster and using ~4 times less memory than any other streaming JSON parser I tried. I'm saying this without any intent to undermine those projects: they are generic JSON parsers while JsonStreamer only does a very specific kind of job.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-07-01