Fast streaming JSON parser for PHP
Go to file
vitalif a40e5fc3cb Merge branch 'unicode-escape-fix' of maxmitti/fast-json-stream into master
Thanks :)
2020-09-07 18:07:42 +03:00
JSONStream.php Fix handling of \u escapes 2020-08-20 16:14:05 +02:00
README.md Now that somebody found this repo, I'm adding a license notice :) 2019-12-24 02:33:27 +03:00

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.