Skip to content

Commit 6acb772

Browse files
committed
Merge branch '2.8' into 3.3
* 2.8: [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR Tweaked some styles in the profiler tables [Security] Fail gracefully if the security token cannot be unserialized from the session [Form] AbstractLayoutTest - fix DOMDocument casing bumped Symfony version to 2.8.34 updated VERSION for 2.8.33 updated CHANGELOG for 2.8.33 bumped Symfony version to 2.7.41 updated VERSION for 2.7.40 update CONTRIBUTORS for 2.7.40 updated CHANGELOG for 2.7.40
2 parents e1e9fd4 + 0af616f commit 6acb772

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Encoder/JsonEncode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function encode($data, $format, array $context = array())
3838

3939
$encodedJson = json_encode($data, $context['json_encode_options']);
4040

41-
if (JSON_ERROR_NONE !== json_last_error()) {
41+
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
4242
throw new UnexpectedValueException(json_last_error_msg());
4343
}
4444

Tests/Encoder/JsonEncoderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ 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+
public function testEncodeNotUtf8WithPartialOnError()
82+
{
83+
$context = array('json_encode_options' => JSON_PARTIAL_OUTPUT_ON_ERROR);
84+
85+
$arr = array(
86+
'utf8' => 'Hello World!',
87+
'notUtf8' => "\xb0\xd0\xb5\xd0",
88+
);
89+
90+
$result = $this->encoder->encode($arr, 'json', $context);
91+
$jsonLastError = json_last_error();
92+
93+
$this->assertSame(JSON_ERROR_UTF8, $jsonLastError);
94+
$this->assertEquals('{"utf8":"Hello World!","notUtf8":null}', $result);
95+
96+
$this->assertEquals('0', $this->serializer->serialize(NAN, 'json', $context));
97+
}
98+
99+
public function testDecodeFalseString()
100+
{
101+
$result = $this->encoder->decode('false', 'json');
102+
$this->assertSame(JSON_ERROR_NONE, json_last_error());
103+
$this->assertFalse($result);
104+
}
105+
68106
protected function getJsonSource()
69107
{
70108
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)