8000 [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT… · symfony/symfony@ae7b33e · GitHub
[go: up one dir, main page]

Skip to content

Commit ae7b33e

Browse files
diversantvlznicolas-grekas
authored andcommitted
[Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR
1 parent 05adcd0 commit ae7b33e

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 && (\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)
0