mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Optimized Model/JsonResource::toJson
(#6963)
This commit is contained in:
parent
3220b80ade
commit
1b2251d138
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,14 +132,16 @@ 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.");
|
||||
}
|
||||
|
||||
if (is_array($decoded)) {
|
||||
$output = array_merge($output, $decoded);
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}, []);
|
||||
|
Loading…
Reference in New Issue
Block a user