stolt/json-lines 问题修复 & 功能扩展

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

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

stolt/json-lines

最新稳定版本:v4.1.0

Composer 安装命令:

composer require stolt/json-lines

包简介

Library for the JSON Lines text file format.

README 文档

README

Test Version PHP Version

This is a library to enline to the JSON Lines format and to deline back from it to JSON.

Installation via Composer

composer require stolt/json-lines

Usage

To enline a data structure into JSON Lines use the enline method.

$jsonLines = (new JsonLines())->enline([
    ["one" => 1, "two" => 2],
    ["three" => 3, "four" => 4, "five" => 5],
    ["six" => 6, "seven" => 7, "key" => "value"],
    ["nested" => ["a", "b", "c"]],
]);
var_dump($jsonLines);

Which will give you the following JSON Lines string.

string(107) "{"one":1,"two":2}
{"three":3,"four":4,"five":5}
{"six":6,"seven":7,"key":"value"}
{"nested":["a","b","c"]}
"

To enline a data structure into a JSON Lines file use the enlineToFile method, adding the gz extension will gzip compress the JSON Lines as shown next.

(new JsonLines())->enlineToFile([
    ["one" => 1, "two" => 2],
    ["three" => 3, "four" => 4, "five" => 5],
    ["six" => 6, "seven" => 7, "key" => "value"],
    ["nested" => ["a", "b", "c"]],
    'out.jsonl.gz'
]);

To deline JSON Lines back into JSON use the deline method.

$json = (new JsonLines())->deline('{"one":1,"two":2}
{"three":3,"four":4,"five":5}
{"six":6,"seven":7,"key":"value"}
{"nested":["a","b","c"]}'
);
var_dump($json)

Which will give you the following JSON string, which is only beautified here to illustrate the data structure.

string(287) "[
    {
        "one": 1,
        "two": 2
    },
    {
        "three": 3,
        "four": 4,
        "five": 5
    },
    {
        "six": 6,
        "seven": 7,
        "key": "value"
    },
    {
        "nested": [
            "a",
            "b",
            "c"
        ]
    }
]"

To deline a complete JSON Lines file back into JSON use the delineFromFile method.

$json = (new JsonLines())->delineFromFile('/path/to/enlined.jsonl');

To deline a complete JSON Lines file line-by-line, use the delineEachLineFromFile method. This allows to iterate over a large file without storing the entire delined file in memory.

$json_lines = (new JsonLines())->delineEachLineFromFile('/path/to/enlined.jsonl');
foreach ($json_lines as $json_line) {
    var_dump($json_line);
}

Running tests

composer test

License

This library is licensed under the MIT license. Please see LICENSE for more details.

Changelog

Please see CHANGELOG for more details.

Contributing

Please see CONTRIBUTING for more details.

统计信息

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

GitHub 信息

  • Stars: 38
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-25