Skip to content

Commit d68e246

Browse files
committed
fix(serializer): json encoder invalid utf8 characters (#4741)
1 parent 4bceeb0 commit d68e246

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Serializer/JsonEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(private readonly string $format, private ?BaseJsonEn
3535

3636
$this->jsonEncoder = new BaseJsonEncoder(
3737
// Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
38-
new JsonEncode(['json_encode_options' => \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE]),
38+
new JsonEncode(['json_encode_options' => \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | \JSON_INVALID_UTF8_IGNORE]),
3939
new JsonDecode(['json_decode_associative' => true])
4040
);
4141
}

tests/Serializer/JsonEncoderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ public function testDecode(): void
5151
{
5252
$this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', 'json'));
5353
}
54+
55+
public function testUTF8EncodedString(): void
56+
{
57+
$data = ['foo' => 'Über'];
58+
59+
$this->assertEquals('{"foo":"Über"}', $this->encoder->encode($data, 'json'));
60+
}
61+
62+
public function testUTF8MalformedHandlingEncoding(): void
63+
{
64+
$data = ['foo' => pack('H*', 'B11111')];
65+
66+
$this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json'));
67+
}
5468
}

0 commit comments

Comments
 (0)