Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json-encoding when JSON_THROW_ON_ERROR is used #31869

Merged
merged 1 commit into from Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -186,11 +186,6 @@ private function writeData(array $data, array $options)
{
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
// Work around https://bugs.php.net/77997
json_encode(null);
}

$this->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
}

Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
Expand Up @@ -99,11 +99,6 @@ private function writeData(array $data, array $options)
{
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
// Work around https://bugs.php.net/77997
json_encode(null);
}

$this->write(json_encode($data, $flags));
}

Expand Down
Expand Up @@ -83,11 +83,6 @@ private function writeData(array $data, array $options)
{
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
// Work around https://bugs.php.net/77997
json_encode(null);
}

$this->output->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
}

Expand Down
9 changes: 4 additions & 5 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -153,11 +153,6 @@ public function setData($data = [])
restore_error_handler();
}
} else {
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
// Work around https://bugs.php.net/77997
json_encode(null);
}

try {
$data = json_encode($data, $this->encodingOptions);
} catch (\Exception $e) {
Expand All @@ -166,6 +161,10 @@ public function setData($data = [])
}
throw $e;
}

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
return $this->setJson($data);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Expand Up @@ -72,7 +72,15 @@ public function decode($data, $format, array $context = [])
$recursionDepth = $context['json_decode_recursion_depth'];
$options = $context['json_decode_options'];

$decodedData = json_decode($data, $associative, $recursionDepth, $options);
try {
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
} catch (\JsonException $e) {
throw new NotEncodableValueException($e->getMessage(), 0, $e);
}

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
return $decodedData;
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new NotEncodableValueException(json_last_error_msg());
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Expand Up @@ -36,13 +36,12 @@ public function encode($data, $format, array $context = [])
{
$context = $this->resolveContext($context);

$encodedJson = json_encode($data, $context['json_encode_options']);

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
// Work around https://bugs.php.net/77997
json_encode(null);
return $encodedJson;
}

$encodedJson = json_encode($data, $context['json_encode_options']);

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
throw new NotEncodableValueException(json_last_error_msg());
}
Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Component/Translation/Dumper/JsonFileDumper.php
Expand Up @@ -31,11 +31,6 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
$flags = \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
}

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
// Work around https://bugs.php.net/77997
json_encode(null);
}

return json_encode($messages->all($domain), $flags);
}

Expand Down