diff --git a/src/Serializer/AbstractSerializer.php b/src/Serializer/AbstractSerializer.php index 42946bf8d..9e6312325 100644 --- a/src/Serializer/AbstractSerializer.php +++ b/src/Serializer/AbstractSerializer.php @@ -216,6 +216,10 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = []) */ protected function serializeString(string $value): string { + if ($value === '') { + return ''; + } + // we always guarantee this is coerced, even if we can't detect encoding if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) { $encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: ''; diff --git a/tests/Serializer/SerializerTest.php b/tests/Serializer/SerializerTest.php index d402f210f..3f0662027 100644 --- a/tests/Serializer/SerializerTest.php +++ b/tests/Serializer/SerializerTest.php @@ -225,6 +225,15 @@ public function testClippingUTF8Characters(): void $this->assertSame(\JSON_ERROR_NONE, json_last_error()); } + public function testEmptyString(): void + { + $serializer = $this->createSerializer(); + + $result = $this->invokeSerialization($serializer, ''); + + $this->assertSame('', $result); + } + /** * @return Serializer */