Skip to content

Commit fad59b3

Browse files
bug symfony#25490 [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR (diversantvlz)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | no <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Replace this comment by a description of what your PR is solving. --> Php function json_encode/decode with option JSON_PARTIAL_OUTPUT_ON_ERROR return result on error, but if have is error json_last_error() always return error code even if there is a result and it is not false. Because of this is impossible set JSON_PARTIAL_OUTPUT_ON_ERROR option across variable $context. Current fix solves this problem. Verification on the false is completely correct, since json_encode / decode returns false only on error if not set JSON_PARTIAL_OUTPUT_ON_ERROR option. Such have a problem e.g when encoding data is not utf-8 (emoji from facebook). Commits ------- e7e410b [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR
2 parents f2d687a + e7e410b commit fad59b3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function encode($data, $format, array $context = array())
5555

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

58-
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
58+
$this->lastError = json_last_error();
59+
if (JSON_ERROR_NONE !== $this->lastError && (false === $encodedJson || \PHP_VERSION_ID < 50500 || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
5960
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
6061
}
6162

src/Symfony/Component/Serializer/Tests/Encoder/JsonEncoderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,47 @@ public function testOptions()
6565
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
6666
}
6767

68+
/**
69+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
70+
*/
71+
public function testEncodeNotUtf8WithoutPartialOnError()
72+
{
73+
$arr = array(
74+
'utf8' => 'Hello World!',
75+
'notUtf8' => "\xb0\xd0\xb5\xd0",
76+
);
77+
78+
$this->encoder->encode($arr, 'json');
79+
}
80+
81+
/**
82+
* @requires PHP 5.5
83+
*/
84+
public function testEncodeNotUtf8WithPartialOnError()
85+
{
86+
$context = array('json_encode_options' => JSON_PARTIAL_OUTPUT_ON_ERROR);
87+
88+
$arr = array(
89+
'utf8' => 'Hello World!',
90+
'notUtf8' => "\xb0\xd0\xb5\xd0",
91+
);
92+
93+
$result = $this->encoder->encode($arr, 'json', $context);
94+
$jsonLastError = json_last_error();
95+
96+
$this->assertSame(JSON_ERROR_UTF8, $jsonLastError);
97+
$this->assertEquals('{"utf8":"Hello World!","notUtf8":null}', $result);
98+
99+
$this->assertEquals('0', $this->serializer->serialize(NAN, 'json', $context));
100+
}
101+
102+
public function testDecodeFalseString()
103+
{
104+
$result = $this->encoder->decode('false', 'json');
105+
$this->assertSame(JSON_ERROR_NONE, json_last_error());
106+
$this->assertFalse($result);
107+
}
108+
68109
protected function getJsonSource()
69110
{
70111
return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';

0 commit comments

Comments
 (0)