Skip to content

Commit

Permalink
Catch JsonException and rethrow in JsonEncode
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-davis committed Jun 27, 2019
1 parent eb438a4 commit 9c76790
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Expand Up @@ -35,14 +35,19 @@ public function __construct($bitmask = 0)
public function encode($data, $format, array $context = [])
{
$context = $this->resolveContext($context);
$options = $context['json_encode_options'];

$encodedJson = json_encode($data, $context['json_encode_options']);
try {
$encodedJson = json_encode($data, $options);
} catch (\JsonException $e) {
throw new NotEncodableValueException($e->getMessage(), 0, $e);
}

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
return $encodedJson;
}

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
throw new NotEncodableValueException(json_last_error_msg());
}

Expand Down

0 comments on commit 9c76790

Please sign in to comment.