Skip to content

Commit 92e8514

Browse files
committed
bug symfony#31869 Fix json-encoding when JSON_THROW_ON_ERROR is used (nicolas-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 symfony#31860 by @lt Commits ------- d18f42c Fix json-encoding when JSON_THROW_ON_ERROR is used
2 parents 2ae0580 + d18f42c commit 92e8514

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ private function writeData(array $data, array $options)
186186
{
187187
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
188188

189-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
190-
// Work around https://bugs.php.net/77997
191-
json_encode(null);
192-
}
193-
194189
$this->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
195190
}
196191

src/Symfony/Component/Console/Descriptor/JsonDescriptor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ private function writeData(array $data, array $options)
9999
{
100100
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
101101

102-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
103-
// Work around https://bugs.php.net/77997
104-
json_encode(null);
105-
}
106-
107102
$this->write(json_encode($data, $flags));
108103
}
109104

src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ private function writeData(array $data, array $options)
8383
{
8484
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
8585

86-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
87-
// Work around https://bugs.php.net/77997
88-
json_encode(null);
89-
}
90-
9186
$this->output->write(json_encode($data, $flags | JSON_PRETTY_PRINT)."\n");
9287
}
9388

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,6 @@ public function setData($data = [])
153153
restore_error_handler();
154154
}
155155
} else {
156-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
157-
// Work around https://bugs.php.net/77997
158-
json_encode(null);
159-
}
160-
161156
try {
162157
$data = json_encode($data, $this->encodingOptions);
163158
} catch (\Exception $e) {
@@ -166,6 +161,10 @@ public function setData($data = [])
166161
}
167162
throw $e;
168163
}
164+
165+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
166+
return $this->setJson($data);
167+
}
169168
}
170169
}
171170

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ public function decode($data, $format, array $context = [])
7272
$recursionDepth = $context['json_decode_recursion_depth'];
7373
$options = $context['json_decode_options'];
7474

75-
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
75+
try {
76+
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
77+
} catch (\JsonException $e) {
78+
throw new NotEncodableValueException($e->getMessage(), 0, $e);
79+
}
80+
81+
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
82+
return $decodedData;
83+
}
7684

7785
if (JSON_ERROR_NONE !== json_last_error()) {
7886
throw new NotEncodableValueException(json_last_error_msg());

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public function encode($data, $format, array $context = [])
3636
{
3737
$context = $this->resolveContext($context);
3838

39+
$encodedJson = json_encode($data, $context['json_encode_options']);
40+
3941
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $context['json_encode_options'])) {
40-
// Work around https://bugs.php.net/77997
41-
json_encode(null);
42+
return $encodedJson;
4243
}
4344

44-
$encodedJson = json_encode($data, $context['json_encode_options']);
45-
4645
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
4746
throw new NotEncodableValueException(json_last_error_msg());
4847
}

src/Symfony/Component/Translation/Dumper/JsonFileDumper.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
3131
$flags = \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
3232
}
3333

34-
if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $flags)) {
35-
// Work around https://bugs.php.net/77997
36-
json_encode(null);
37-
}
38-
3934
return json_encode($messages->all($domain), $flags);
4035
}
4136

0 commit comments

Comments
 (0)