Skip to content

Commit bfae6fd

Browse files
divineAntonioCS
andauthored
fix(serializer): json encoder invalid utf8 characters (#4741)
* fix: json encoder invalid utf8 characters Co-Authored-By: AntonioCS <[email protected]> * fix: skip test for php 7.1 Co-authored-by: AntonioCS <[email protected]>
1 parent f81e3e8 commit bfae6fd

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Serializer/JsonEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null)
4040
}
4141

4242
// Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
43-
$jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE;
43+
$jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | (\PHP_VERSION_ID >= 70200 ? \JSON_INVALID_UTF8_IGNORE : 0);
4444
if (interface_exists(AdvancedNameConverterInterface::class)) {
4545
$jsonEncode = new JsonEncode(['json_encode_options' => $jsonEncodeOptions]);
4646
$jsonDecode = new JsonDecode(['json_decode_associative' => true]);

tests/Serializer/JsonEncoderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ public function testDecode()
5454
{
5555
$this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', 'json'));
5656
}
57+
58+
public function testUTF8EncodedString()
59+
{
60+
$data = ['foo' => 'Über'];
61+
62+
$this->assertEquals('{"foo":"Über"}', $this->encoder->encode($data, 'json'));
63+
}
64+
65+
public function testUTF8MalformedHandlingEncoding()
66+
{
67+
if (\PHP_VERSION_ID >= 70200) {
68+
$data = ['foo' => pack('H*', 'B11111')];
69+
70+
$this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json'));
71+
}
72+
}
5773
}

0 commit comments

Comments
 (0)