Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove json utils methods and json custom exception #2133

Open
wants to merge 2 commits into
base: 8.x
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Bulk/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Elastica\Bulk;
use Elastica\Index;
use Elastica\JSON;

class Action
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
12 changes: 0 additions & 12 deletions src/Exception/JSONParseException.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Exception/PartialShardFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Elastica\Exception;

use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand All @@ -21,6 +20,6 @@ public function __construct(Request $request, Response $response)
parent::__construct($request, $response);

$shardsStatistics = $response->getShardsStatistics();
$this->message = JSON::stringify($shardsStatistics);
$this->message = \json_encode($shardsStatistics, \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR);
}
}
85 changes: 0 additions & 85 deletions src/JSON.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/Multi/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Elastica\Multi;

use Elastica\Client;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Search as BaseSearch;

Expand Down Expand Up @@ -161,8 +160,8 @@ protected function _getSearchData(BaseSearch $search): string
// Keep other query options as part of the search body
$queryOptions = \array_diff_key($search->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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Transport/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/Transport/HttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Transport/NullTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Elastica\Transport;

use Elastica\JSON;
use Elastica\Request;
use Elastica\Response;

Expand Down Expand Up @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions tests/Exception/JSONParseExceptionTest.php

This file was deleted.

3 changes: 1 addition & 2 deletions tests/Exception/PartialShardFailureExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Elastica\Document;
use Elastica\Exception\PartialShardFailureException;
use Elastica\JSON;
use Elastica\Query;
use Elastica\ResultSet\DefaultBuilder;

Expand Down Expand Up @@ -61,7 +60,7 @@ public function testPartialFailure(): void
$resultSet = $builder->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');
}
Expand Down
43 changes: 0 additions & 43 deletions tests/JSONTest.php

This file was deleted.