Skip to content

Commit 7ed64c1

Browse files
authored
fix: displaying "encoding error" when serializing empty strings (#1940)
1 parent 1de5202 commit 7ed64c1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Serializer/AbstractSerializer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = [])
216216
*/
217217
protected function serializeString(string $value): string
218218
{
219+
if ($value === '') {
220+
return '';
221+
}
222+
219223
// we always guarantee this is coerced, even if we can't detect encoding
220224
if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) {
221225
$encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: '<encoding error>';

tests/Serializer/SerializerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ public function testClippingUTF8Characters(): void
225225
$this->assertSame(\JSON_ERROR_NONE, json_last_error());
226226
}
227227

228+
public function testEmptyString(): void
229+
{
230+
$serializer = $this->createSerializer();
231+
232+
$result = $this->invokeSerialization($serializer, '');
233+
234+
$this->assertSame('', $result);
235+
}
236+
228237
/**
229238
* @return Serializer
230239
*/

0 commit comments

Comments
 (0)