diff --git a/src/Decoder.php b/src/Decoder.php index 876ff2b..e3970f1 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -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) { diff --git a/tests/DecoderTest.php b/tests/DecoderTest.php index 0743e6b..c72749a 100644 --- a/tests/DecoderTest.php +++ b/tests/DecoderTest.php @@ -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'))); @@ -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()