diff --git a/src/Serializer/JsonEncoder.php b/src/Serializer/JsonEncoder.php index 2a95e510bb0..53fa2f8aa1f 100644 --- a/src/Serializer/JsonEncoder.php +++ b/src/Serializer/JsonEncoder.php @@ -40,7 +40,7 @@ public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null) } // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML. - $jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE; + $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); if (interface_exists(AdvancedNameConverterInterface::class)) { $jsonEncode = new JsonEncode(['json_encode_options' => $jsonEncodeOptions]); $jsonDecode = new JsonDecode(['json_decode_associative' => true]); diff --git a/tests/Serializer/JsonEncoderTest.php b/tests/Serializer/JsonEncoderTest.php index a4138b3fffd..2be8c917f96 100644 --- a/tests/Serializer/JsonEncoderTest.php +++ b/tests/Serializer/JsonEncoderTest.php @@ -54,4 +54,20 @@ public function testDecode() { $this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', 'json')); } + + public function testUTF8EncodedString() + { + $data = ['foo' => 'Über']; + + $this->assertEquals('{"foo":"Über"}', $this->encoder->encode($data, 'json')); + } + + public function testUTF8MalformedHandlingEncoding() + { + if (\PHP_VERSION_ID >= 70200) { + $data = ['foo' => pack('H*', 'B11111')]; + + $this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json')); + } + } }