Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public function handleData($data)
$data = (string)\substr($this->buffer, 0, $newline);
$this->buffer = (string)\substr($this->buffer, $newline + 1);

// decode data with simdjson when the extension is available
if (\extension_loaded('simdjson')) {
try {
$data = \simdjson_decode($data, $this->assoc, $this->depth);

$this->emit('data', array($data));
} catch (\Throwable $err) {
$this->handleError(new \RuntimeException('Unable to decode JSON: ' . $err->getMessage(), $err->getCode(), $err));
}

continue;
}

// decode data with options given in ctor
// @codeCoverageIgnoreStart
if ($this->options === 0) {
Expand Down
9 changes: 7 additions & 2 deletions tests/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function testEmitDataBigIntOptionWillForwardAsString()
if (!defined('JSON_BIGINT_AS_STRING')) {
$this->markTestSkipped('Const JSON_BIGINT_AS_STRING only available in PHP 5.4+');
}

if (\extension_loaded('simdjson')) {
$this->markTestSkipped('Flags cannot be passed to simdjson_decode');
}

$this->decoder = new Decoder($this->input, false, 512, JSON_BIGINT_AS_STRING);
$this->decoder->on('data', $this->expectCallableOnceWith($this->identicalTo('999888777666555444333222111000')));

Expand All @@ -87,8 +92,8 @@ public function testEmitDataErrorWillForwardError()
$this->input->emit('data', array("invalid\n"));

$this->assertInstanceOf('RuntimeException', $error);
$this->assertContainsString('Syntax error', $error->getMessage());
$this->assertEquals(JSON_ERROR_SYNTAX, $error->getCode());
$this->assertMatchesRegularExpression('/(syntax error|improper structure)/i', $error->getMessage());
$this->assertEquals(\extension_loaded('simdjson') ? SIMDJSON_ERR_TAPE_ERROR : JSON_ERROR_SYNTAX, $error->getCode());
}

public function testEmitDataErrorWillForwardErrorAlsoWhenCreatedWithThrowOnError()
Expand Down
Loading