Skip to content

Commit

Permalink
Optimized Model/JsonResource::toJson (#6963)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Jul 22, 2024
1 parent 4653e6c commit ce2b2b7
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -889,10 +890,10 @@ public function toArray(): array
*/
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;
Expand Down

0 comments on commit ce2b2b7

Please sign in to comment.