1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00

Add line delimited JSON parser (#65)

This commit is contained in:
Niklas Keller 2019-08-22 23:36:28 +02:00 committed by GitHub
parent 38d13dbb5c
commit 1b84c81bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 1 deletions

View File

@ -2,7 +2,9 @@
namespace Amp\ByteStream;
use Amp\Iterator;
use Amp\Loop;
use Amp\Producer;
use Amp\Promise;
use function Amp\call;
@ -99,6 +101,7 @@ function getOutputBufferStream(): ResourceOutputStream
return $stream;
}
/**
* The STDIN stream for the process associated with the currently active event loop.
*
@ -155,3 +158,31 @@ function getStderr(): ResourceOutputStream
return $stream;
}
function parseLineDelimitedJson(InputStream $stream, bool $assoc = false, int $depth = 512, int $options = 0): Iterator
{
return new Producer(static function (callable $emit) use ($stream, $assoc, $depth, $options) {
$reader = new LineReader($stream);
while (null !== $line = yield $reader->readLine()) {
$line = \trim($line);
if ($line === '') {
continue;
}
/** @noinspection PhpComposerExtensionStubsInspection */
$data = \json_decode($line, $assoc, $depth, $options);
/** @noinspection PhpComposerExtensionStubsInspection */
$error = \json_last_error();
/** @noinspection PhpComposerExtensionStubsInspection */
if ($error !== \JSON_ERROR_NONE) {
/** @noinspection PhpComposerExtensionStubsInspection */
throw new StreamException('Failed to parse JSON: ' . \json_last_error_msg(), $error);
}
yield $emit($data);
}
});
}

View File

@ -0,0 +1,38 @@
<?php
/** @noinspection PhpComposerExtensionStubsInspection */
/** @noinspection PhpUnhandledExceptionInspection */
namespace Amp\ByteStream\Test;
use Amp\ByteStream\InMemoryStream;
use Amp\ByteStream\StreamException;
use Amp\Iterator;
use Amp\PHPUnit\TestCase;
use function Amp\ByteStream\parseLineDelimitedJson;
use function Amp\Promise\wait;
class ParseLineDelimitedJsonTest extends TestCase
{
public function test()
{
$result = wait(Iterator\toArray(parseLineDelimitedJson(new InMemoryStream(\implode("\n", [
\json_encode(['foo' => "\nbar\r\n"]),
\json_encode(['foo' => []]),
])))));
self::assertEquals([
(object) ['foo' => "\nbar\r\n"],
(object) ['foo' => []],
], $result);
}
public function testInvalidJson()
{
$this->expectException(StreamException::class);
$this->expectExceptionMessage('Failed to parse JSON');
wait(Iterator\toArray(parseLineDelimitedJson(new InMemoryStream('{'))));
}
}