Catch JsonException and rethrow in JsonEncode

This commit is contained in:
Phil Davis 2019-06-27 09:45:05 +05:45
parent eb438a48d6
commit 9c76790ee8
1 changed files with 8 additions and 3 deletions

View File

@ -35,14 +35,19 @@ class JsonEncode implements EncoderInterface
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());
}