Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c76790

Browse files
committed
Catch JsonException and rethrow in JsonEncode
1 parent eb438a4 commit 9c76790

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/Symfony/Component/Serializer/Encoder/JsonEncode.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ public function __construct($bitmask = 0)
3535
public function encode($data, $format, array $context = [])
3636
{
3737
$context = $this->resolveContext($context);
38+
$options = $context['json_encode_options'];
3839

39-
$encodedJson = json_encode($data, $context['json_encode_options']);
40+
try {
41+
$encodedJson = json_encode($data, $options);
42+
} catch (\JsonException $e) {
43+
throw new NotEncodableValueException($e->getMessage(), 0, $e);
44+
}
4045

41-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
46+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
4247
return $encodedJson;
4348
}
4449

45-
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
50+
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
4651
throw new NotEncodableValueException(json_last_error_msg());
4752
}
4853

0 commit comments

Comments
 (0)