From aadaed28d886579edf458843956d1b81efbe4c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Wed, 7 Dec 2022 14:44:19 +0100 Subject: [PATCH 1/2] Remove json utils methods and json custom exception --- src/Bulk/Action.php | 5 +- src/Exception/JSONParseException.php | 12 --- .../PartialShardFailureException.php | 3 +- src/JSON.php | 85 ------------------- src/Multi/Search.php | 5 +- src/Request.php | 2 +- src/Response.php | 4 +- src/Transport/Guzzle.php | 3 +- src/Transport/Http.php | 3 +- src/Transport/HttpAdapter.php | 3 +- src/Transport/NullTransport.php | 3 +- src/Util.php | 2 +- tests/Exception/JSONParseExceptionTest.php | 10 --- .../PartialShardFailureExceptionTest.php | 3 +- tests/JSONTest.php | 43 ---------- 15 files changed, 14 insertions(+), 172 deletions(-) delete mode 100644 src/Exception/JSONParseException.php delete mode 100644 src/JSON.php delete mode 100644 tests/Exception/JSONParseExceptionTest.php delete mode 100644 tests/JSONTest.php diff --git a/src/Bulk/Action.php b/src/Bulk/Action.php index d97fd82aeb..9a8a3eb7e8 100644 --- a/src/Bulk/Action.php +++ b/src/Bulk/Action.php @@ -4,7 +4,6 @@ use Elastica\Bulk; use Elastica\Index; -use Elastica\JSON; class Action { @@ -47,7 +46,7 @@ public function __construct(string $opType = self::OP_TYPE_INDEX, array $metadat public function __toString(): string { - $string = JSON::stringify($this->getActionMetadata(), \JSON_FORCE_OBJECT).Bulk::DELIMITER; + $string = \json_encode($this->getActionMetadata(), \JSON_FORCE_OBJECT | \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR).Bulk::DELIMITER; if ($this->hasSource()) { $source = $this->getSource(); @@ -61,7 +60,7 @@ public function __toString(): string } $string .= '{"doc": '.$source['doc'].$docAsUpsert.'}'; } else { - $string .= JSON::stringify($source, \JSON_UNESCAPED_UNICODE); + $string .= \json_encode($source, \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } $string .= Bulk::DELIMITER; } diff --git a/src/Exception/JSONParseException.php b/src/Exception/JSONParseException.php deleted file mode 100644 index cbb0e2ca81..0000000000 --- a/src/Exception/JSONParseException.php +++ /dev/null @@ -1,12 +0,0 @@ -getShardsStatistics(); - $this->message = JSON::stringify($shardsStatistics); + $this->message = \json_encode($shardsStatistics, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } } diff --git a/src/JSON.php b/src/JSON.php deleted file mode 100644 index 58d46aeacc..0000000000 --- a/src/JSON.php +++ /dev/null @@ -1,85 +0,0 @@ -getOptions(), \array_flip(self::$HEADER_OPTIONS)); - $data = JSON::stringify($header)."\n"; - $data .= JSON::stringify($query->toArray() + $queryOptions)."\n"; + $data = \json_encode($header, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR)."\n"; + $data .= \json_encode($query->toArray() + $queryOptions, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR)."\n"; return $data; } diff --git a/src/Request.php b/src/Request.php index 9985443edb..94ca77af9e 100644 --- a/src/Request.php +++ b/src/Request.php @@ -50,7 +50,7 @@ public function __construct(string $path, string $method = self::GET, $data = [] public function __toString(): string { - return JSON::stringify($this->toArray()); + return \json_encode($this->toArray(), \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } /** diff --git a/src/Response.php b/src/Response.php index e6fdd53e71..d22448cc23 100644 --- a/src/Response.php +++ b/src/Response.php @@ -350,9 +350,9 @@ private function getResponseString() } try { if ($this->getJsonBigintConversion()) { - $response = JSON::parse($response, true, 512, \JSON_BIGINT_AS_STRING); + $response = \json_decode($response, true, flags: \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); } else { - $response = JSON::parse($response); + $response = $response = \json_decode($response, true, flags: \JSON_THROW_ON_ERROR); } } catch (\JsonException $e) { // leave response as is if parse fails diff --git a/src/Transport/Guzzle.php b/src/Transport/Guzzle.php index fd9d9d5ba9..57967ce739 100755 --- a/src/Transport/Guzzle.php +++ b/src/Transport/Guzzle.php @@ -6,7 +6,6 @@ use Elastica\Exception\Connection\GuzzleException; use Elastica\Exception\PartialShardFailureException; use Elastica\Exception\ResponseException; -use Elastica\JSON; use Elastica\Request; use Elastica\Response; use Elastica\Util; @@ -201,7 +200,7 @@ protected function _getActionPath(Request $request): string private function streamFor($data): StreamInterface { if (\is_array($data)) { - $data = JSON::stringify($data, \JSON_UNESCAPED_UNICODE); + $data = \json_encode($data, \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } return \class_exists(Psr7\Utils::class) diff --git a/src/Transport/Http.php b/src/Transport/Http.php index eaf2140b25..1f8255b0f7 100644 --- a/src/Transport/Http.php +++ b/src/Transport/Http.php @@ -6,7 +6,6 @@ use Elastica\Exception\ConnectionException; use Elastica\Exception\PartialShardFailureException; use Elastica\Exception\ResponseException; -use Elastica\JSON; use Elastica\Request; use Elastica\Response; use Elastica\Util; @@ -128,7 +127,7 @@ public function exec(Request $request, array $params): Response } if (\is_array($data)) { - $content = JSON::stringify($data, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES); + $content = \json_encode($data, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } else { $content = $data; diff --git a/src/Transport/HttpAdapter.php b/src/Transport/HttpAdapter.php index 573faa8eb7..69f2878d32 100644 --- a/src/Transport/HttpAdapter.php +++ b/src/Transport/HttpAdapter.php @@ -5,7 +5,6 @@ use Elastica\Connection; use Elastica\Exception\PartialShardFailureException; use Elastica\Exception\ResponseException; -use Elastica\JSON; use Elastica\Request as ElasticaRequest; use Elastica\Response; use Elastica\Response as ElasticaResponse; @@ -109,7 +108,7 @@ protected function _createHttpAdapterRequest(ElasticaRequest $elasticaRequest, C } if (\is_array($data)) { - $body = JSON::stringify($data, \JSON_UNESCAPED_UNICODE); + $body = \json_encode($data, \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR); } else { $body = $data; } diff --git a/src/Transport/NullTransport.php b/src/Transport/NullTransport.php index add545f666..6760b59a18 100644 --- a/src/Transport/NullTransport.php +++ b/src/Transport/NullTransport.php @@ -2,7 +2,6 @@ namespace Elastica\Transport; -use Elastica\JSON; use Elastica\Request; use Elastica\Response; @@ -73,7 +72,7 @@ public function generateDefaultResponse(array $params): Response 'params' => $params, ]; - return new Response(JSON::stringify($response)); + return new Response(\json_encode($response, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR)); } /** diff --git a/src/Util.php b/src/Util.php index 3837a5622a..3fa2e66541 100644 --- a/src/Util.php +++ b/src/Util.php @@ -245,7 +245,7 @@ public static function convertRequestToCurlCommand(Request $request) $data = $request->getData(); if (!empty($data)) { - $message .= ' -d \''.JSON::stringify($data).'\''; + $message .= ' -d \''.\json_encode($data, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR).'\''; } return $message; diff --git a/tests/Exception/JSONParseExceptionTest.php b/tests/Exception/JSONParseExceptionTest.php deleted file mode 100644 index 3d36de7622..0000000000 --- a/tests/Exception/JSONParseExceptionTest.php +++ /dev/null @@ -1,10 +0,0 @@ -buildResultSet($e->getResponse(), $query); $this->assertCount(0, $resultSet->getResults()); - $message = JSON::parse($e->getMessage()); + $message = \json_decode($e->getMessage(), true, flags: \JSON_THROW_ON_ERROR); $this->assertArrayHasKey('failures', $message, 'Failures are absent'); $this->assertGreaterThan(0, \count($message['failures']), 'Failures are empty'); } diff --git a/tests/JSONTest.php b/tests/JSONTest.php deleted file mode 100644 index ad17404ddc..0000000000 --- a/tests/JSONTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * @internal - */ -class JSONTest extends TestCase -{ - public function testStringifyMustNotThrowExceptionOnValid(): void - { - JSON::stringify([]); - $this->assertTrue(true); - } - - public function testStringifyMustThrowExceptionNanOrInf(): void - { - $this->expectException(JSONParseException::class); - $this->expectExceptionMessage('Inf and NaN cannot be JSON encoded'); - - $arr = [\NAN, \INF]; - JSON::stringify($arr); - $this->assertTrue(true); - } - - public function testStringifyMustThrowExceptionMaximumDepth(): void - { - $this->expectException(JSONParseException::class); - $this->expectExceptionMessage('Maximum stack depth exceeded'); - - $arr = [[[]]]; - JSON::stringify($arr, 0, 0); - $this->assertTrue(true); - } -} From 64d6fa5d08767d0e80a3b0835c0c3585462c5e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20de=20Guillebon?= Date: Wed, 7 Dec 2022 14:48:56 +0100 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6760d1ca2..a862b473ce 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/ruflin/Elastica/compare/7.3.0...master) +## [Unreleased](https://github.com/ruflin/Elastica/compare/7.3.0...8.x) ### Backward Compatibility Breaks * Dropped support for PHP <8.0 [#2131](https://github.com/ruflin/Elastica/pull/2131) +* Dropped `Elastica\JSON` and `Elastica\Exception\JSONParseException`. Catch native `\JsonException` instead. [#2133](https://github.com/ruflin/Elastica/pull/2133) ### Added ### Changed ### Deprecated