Skip to content

Commit fe13a1f

Browse files
committed
Merge pull request #510 from Soullivaneuh/yaml-empty-array
Manage empty array for serializer
2 parents 4835aa3 + a6443ad commit fe13a1f

5 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/JMS/Serializer/YamlSerializationVisitor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public function visitArray($data, array $type, Context $context)
111111
->rtrim(false)
112112
->writeln(' {}')
113113
;
114+
} elseif (empty($data)) {
115+
$this->writer
116+
->rtrim(false)
117+
->writeln(' []')
118+
;
114119
}
115120
}
116121

tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ public function testArrayIntegers()
240240
}
241241
}
242242

243+
public function testArrayEmpty()
244+
{
245+
if ('xml' === $this->getFormat()) {
246+
$this->markTestSkipped('XML can\'t be tested for empty array');
247+
}
248+
249+
$data = array('array' => []);
250+
$this->assertEquals($this->getContent('array_empty'), $this->serialize($data));
251+
252+
if ($this->hasDeserializer()) {
253+
$this->assertEquals($data, $this->deserialize($this->getContent('array_empty')), 'array');
254+
}
255+
}
256+
243257
public function testArrayFloats()
244258
{
245259
$data = array(1.34, 3.0, 6.42);

tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function getContent($key)
4646
$outputs['array_strings'] = '["foo","bar"]';
4747
$outputs['array_booleans'] = '[true,false]';
4848
$outputs['array_integers'] = '[1,3,4]';
49+
$outputs['array_empty'] = '{"array":[]}';
4950
$outputs['array_floats'] = '[1.34,3,6.42]';
5051
$outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
5152
$outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
array: []

tests/JMS/Serializer/Tests/Serializer/yml/blog_post_unauthored.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: 'This is a nice title.'
33
created_at: '2011-07-30T00:00:00+0000'
44
is_published: false
55
etag: 1edf9bf60a32d89afbb85b2be849e3ceed5f5b10
6+
comments: []
7+
comments2: []
68
metadata:
79
foo: bar
810
author: null

0 commit comments

Comments
 (0)