Skip to content

Commit

Permalink
bug #31869 Fix json-encoding when JSON_THROW_ON_ERROR is used (nicola…
Browse files Browse the repository at this point in the history
…s-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

Fix json-encoding when JSON_THROW_ON_ERROR is used

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

As hinted in #31860 by @lt

Commits
-------

d18f42c Fix json-encoding when JSON_THROW_ON_ERROR is used
  • Loading branch information
fabpot committed Jun 5, 2019
2 parents 2ae0580 + d18f42c commit 92e8514
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 30 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
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

0 comments on commit 92e8514

Please sign in to comment.