Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3e75b6e

Browse filesBrowse files
diversantvlznicolas-grekas
authored andcommitted
[Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR
1 parent 05adcd0 commit 3e75b6e
Copy full SHA for 3e75b6e

File tree

2 files changed

+41
-1
lines changed
Filter options

2 files changed

+41
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/JsonEncode.php
+2-1Lines 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) {
5960
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
6061
}
6162

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/JsonEncoderTest.php
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,45 @@ public function testOptions()
6565
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
6666
}
6767

68+
public function testEncodeNotUtf8WithoutPartialOnError()
69+
{
70+
$arr = array(
71+
'utf8' => 'Hello World!',
72+
'notUtf8' => "\xb0\xd0\xb5\xd0",
73+
);
74+
75+
$this->expectException(\Exception::class);
76+
$this->encoder->encode($arr, 'json');
77+
}
78+
79+
/**
80+
* @requires PHP 5.5
81+
*/
82+
public function testEncodeNotUtf8WithPartialOnError()
83+
{
84+
$context = array('json_encode_options' => JSON_PARTIAL_OUTPUT_ON_ERROR);
85+
86+
$arr = array(
87+
'utf8' => 'Hello World!',
88+
'notUtf8' => "\xb0\xd0\xb5\xd0",
89+
);
90+
91+
$result = $this->encoder->encode($arr, 'json', $context);
92+
$jsonLastError = json_last_error();
93+
94+
$this->assertSame(JSON_ERROR_UTF8, $jsonLastError);
95+
$this->assertEquals('{"utf8":"Hello World!","notUtf8":null}', $result);
96+
97+
$this->assertEquals('0', $this->serializer->serialize(NAN, 'json', $context));
98+
}
99+
100+
public function testDecodeFalseString()
101+
{
102+
$result = $this->encoder->decode('false', 'json');
103+
$this->assertSame(JSON_ERROR_NONE, json_last_error());
104+
$this->assertFalse($result);
105+
}
106+
68107
protected function getJsonSource()
69108
{
70109
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.