Optimized Model/JsonResource::toJson (#6963)

This commit is contained in:
Deeka Wong 2024-07-22 09:50:05 +08:00 committed by GitHub
parent 3220b80ade
commit 1b2251d138
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 31 additions and 18 deletions

View File

@ -1,5 +1,9 @@
# v3.1.33 - TBD
## Optimized
-[#6963](https://github.com/hyperf/hyperf/pull/6963) Optimized `Model/JsonResource::toJson`.
## Fixed
- [#6954](https://github.com/hyperf/hyperf/pull/6954) Fixed bug that the connection cannot reconnect to the server in a situation where there was a failover and exchange of read and write hosts.

View File

@ -25,6 +25,7 @@ use Hyperf\Database\Model\Relations\Pivot;
use Hyperf\Database\Query\Builder as QueryBuilder;
use Hyperf\Stringable\Str;
use Hyperf\Stringable\StrCache;
use JsonException;
use JsonSerializable;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\StoppableEventInterface;
@ -889,10 +890,10 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*/
public function toJson($options = 0)
{
$json = json_encode($this->jsonSerialize(), $options);
if (json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodingException::forModel($this, json_last_error_msg());
try {
$json = json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw JsonEncodingException::forModel($this, $e->getMessage());
}
return $json;

View File

@ -13,6 +13,7 @@ declare(strict_types=1);
namespace Hyperf\Paginator;
use Hyperf\Contract\Arrayable;
use JsonException;
use UnexpectedValueException;
use function Hyperf\Collection\collect;
@ -95,9 +96,9 @@ class Cursor implements Arrayable
return null;
}
$parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedString)), true);
if (json_last_error() !== JSON_ERROR_NONE) {
try {
$parameters = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $encodedString)), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return null;
}

View File

@ -18,6 +18,7 @@ use Hyperf\Phar\Ast\Ast;
use Hyperf\Phar\Ast\Visitor\RewriteConfigFactoryVisitor;
use Hyperf\Phar\Ast\Visitor\RewriteConfigVisitor;
use InvalidArgumentException;
use JsonException;
use Phar;
use Psr\Log\LoggerInterface;
use RuntimeException;
@ -388,10 +389,12 @@ EOD;
*/
private function loadJson(string $path): array
{
$result = json_decode(file_get_contents($path), true);
if ($result === null) {
throw new InvalidArgumentException(sprintf('Unable to parse given path %s', $path), json_last_error());
try {
$result = json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new InvalidArgumentException(sprintf('Unable to parse given path %s', $path), $e->getCode(), $e);
}
return $result;
}

View File

@ -20,6 +20,7 @@ use Hyperf\Resource\Concerns\ConditionallyLoadsAttributes;
use Hyperf\Resource\Concerns\DelegatesToResource;
use Hyperf\Resource\JsonEncodingException;
use Hyperf\Resource\Response\Response;
use JsonException;
use JsonSerializable;
use Psr\Http\Message\ResponseInterface;
@ -117,10 +118,10 @@ class JsonResource implements ArrayAccess, JsonSerializable, Arrayable, Jsonable
*/
public function toJson(int $options = 0): string
{
$json = json_encode($this->jsonSerialize(), $options);
if (json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodingException::forResource($this, json_last_error_msg());
try {
$json = json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw JsonEncodingException::forResource($this, $e->getMessage());
}
return $json;

View File

@ -14,6 +14,7 @@ namespace Hyperf\Translation;
use Hyperf\Contract\TranslatorLoaderInterface;
use Hyperf\Support\Filesystem\Filesystem;
use JsonException;
use RuntimeException;
use function Hyperf\Collection\collect;
@ -131,13 +132,15 @@ class FileLoader implements TranslatorLoaderInterface
return collect(array_merge($this->jsonPaths, [$this->path]))
->reduce(function ($output, $path) use ($locale) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
try {
$decoded = json_decode($this->files->get($full), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}
$output = array_merge($output, $decoded);
if (is_array($decoded)) {
$output = array_merge($output, $decoded);
}
}
return $output;