indykoning/php-jsonl
Composer 安装命令:
composer require indykoning/php-jsonl
包简介
PHP JSONL library for fast JSON Lines parsing and writing.
关键字:
README 文档
README
This package implements the JSON Lines format allowing you to encode and decode jsonl values. To take full advantage of the format we use PHP's Generators
Installation via Composer
composer require indykoning/php-jsonl
Usage
Decoding
To decode jsonl you can use the decode:
\Indykoning\Jsonl\Jsonl::decode($jsonl, $associative);
The jsonl variable can be a string of jsonl lines, an array of jsonl lines, or any Traversable. So anything that can be iterated over and contains valid json can be passed.
If you want to decode the jsonl from a file, or decode it as you're downloading it you can use decodeFromResource
\Indykoning\Jsonl\Jsonl::decodeFromResource($resource, $associative);
Some examples you can use for this are:
$data = \Indykoning\Jsonl\Jsonl::decodeFromResource(fopen('/tmp/data.jsonl', 'r'), true); $data = \Indykoning\Jsonl\Jsonl::decodeFromResource( \Illuminate\Support\Facades\Http::get('https://example.com/data.jsonl')->resource() );
Encoding
Encoding can be done by calling encode with a Traversable or array with the objects inside
\Indykoning\Jsonl\Jsonl::encode($array); \Indykoning\Jsonl\Jsonl::encode([ ['name' => 'Gilbert', 'session' => '2013', 'score' => 24, 'completed' => true], ['name' => 'Alexa', 'session' => '2013', 'score' => 29, 'completed' => true], ]);
Encoding also supports writing directly to a resource using encodeToResource:
\Indykoning\Jsonl\Jsonl::encodeToResource($resource, $array); \Indykoning\Jsonl\Jsonl::encodeToResource( fopen('/tmp/data.jsonl', 'w'), [ ['name' => 'Gilbert', 'session' => '2013', 'score' => 24, 'completed' => true], ['name' => 'Alexa', 'session' => '2013', 'score' => 29, 'completed' => true], ] );
Example using both
In practice this means you could proxy a theoretically infititely long file in json without issue.
function enrichData($data) { foreach($data as $object) { $object->enriched_data = EnrichmentModel::find($object->id)->toArray(); yield $object; } } response()->streamDownload( function () { $data = \Indykoning\Jsonl\Jsonl::decodeFromResource( \Illuminate\Support\Facades\Http::get('https://example.com/data.jsonl')->resource() ); $data = enrichData($data); foreach(\Indykoning\Jsonl\Jsonl::encode($data) as $chunk) { echo $chunk; ob_flush(); flush(); } }, 'data.jsonl', [ 'Content-Type' => 'application/jsonl', 'X-Accel-Buffering' => 'no' ] );
With this code we'll be dealing with every json line one by one, enriching the data and passing it on to the client. When the client stops requesting data we will stop requesting and enriching data.
Why not package X or Y instead?
I haven't found any php Json Lines package that deals with JSON Lines in a natural and efficient way. Either relying on files to encode/decode JSON. Or loading the entire File or data into memory first, which is a problem for big data.
This package allows extremely simple encoding/decoding, taking advantage of streaming and generators to reduce memory footprint.
Running tests
composer test
License
This library is licensed under the MIT license. Please see LICENSE for more details.
indykoning/php-jsonl 适用场景与选型建议
indykoning/php-jsonl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.86k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2025 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「json」 「jsonlines」 「JSON Lines」 「NDJSON」 「ldjson」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 indykoning/php-jsonl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 indykoning/php-jsonl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 indykoning/php-jsonl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.
Library for the JSON Lines text file format.
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 21.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-05