forked from vitalif/fast-json-stream
Vitaliy Filippov 4193dbb96c | ||
---|---|---|
JSONStream.php | ||
README.md |
README.md
Fast PHP Streaming JSON parser
I found 2 implementations of streaming JSON parsers for PHP:
https://github.com/skolodyazhnyy/json-stream
https://github.com/salsify/jsonstreamingparser
Both are bad:
- json-stream reads and parses the input stream character by character.
- jsonstreamingparser reads the input stream line by line, but still parses it character by character.
- jsonstreamingparser is SAX-like, inconvenient to use.
- both require filehandle to work, can't work with mock function.
Quick tests show that json-stream requires 4.18s to parse a sample 13MB json file, jsonstreamingparser is even worse and requires 6.05s.
This library does the same thing in 1.04s.
No composer, PHP is not the place for npm.
Usage
[enterArray|enterObject ...] readValue, readValue, readValue [exitArray|exitObject]
<?php
require_once './JSONStream.php';
$fp = fopen('datasets.json', 'r');
$json = new JSONStream(function() use($fp) { return fread($fp, 262144); });
$json->enterArray();
while (!$json->isEnded())
{
$json->enterObject();
while ($json->readValue($k))
{
$json->readValue($v);
if ($k == 'Caption')
{
print "$v\n";
}
}
$json->exitObject();
}
$json->exitArray();
fclose($fp);
$fp = fopen('datasets.json', 'r');
$json = new JSONStream(function() use($fp) { return fread($fp, 262144); });
$json->enterArray();
while (!$json->isEnded())
{
$json->readValue($v);
print $v['Caption']."\n";
}
$json->exitArray();
fclose($fp);
Author and license
Author: Vitaliy Filippov, 2018+
License: GNU LGPL 3.0 or MPL (file-level copyleft)
As usual, the software is provided "as is", without the warranty of any kind.