Skip to content

Commit

Permalink
Fix json-encoding when JSON_THROW_ON_ERROR is used
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jun 5, 2019
1 parent 2ae0580 commit 62bd762
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 29 deletions.
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
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

0 comments on commit 62bd762

Please sign in to comment.