Skip to content

Commit

Permalink
Fix serializing nulls in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
megazoll committed Aug 19, 2016
1 parent 8c18b3a commit 0cef8ba
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function visitArray($data, array $type, Context $context)
foreach ($data as $k => $v) {
$v = $this->navigator->accept($v, $this->getElementType($type), $context);

if (null === $v && ( ! is_string($k) || ! $context->shouldSerializeNull())) {
if (null === $v && ! $context->shouldSerializeNull()) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/JMS/Serializer/YamlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function visitArray($data, array $type, Context $context)
|| array_keys($data) === range(0, count($data) - 1);

foreach ($data as $k => $v) {
if (null === $v && ( ! is_string($k) || ! $context->shouldSerializeNull())) {
if (null === $v && ! $context->shouldSerializeNull()) {
continue;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ public function testArrayFloats()
}
}

public function testArrayNulls()
{
$arr = array(null, null);

$this->assertEquals(
$this->getContent('array_nulls'),
$this->serializer->serialize($arr, $this->getFormat(), SerializationContext::create()->setSerializeNull(true))
);
}

public function testArrayObjects()
{
$data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected function getContent($key)
$outputs['array_integers'] = '[1,3,4]';
$outputs['array_empty'] = '{"array":[]}';
$outputs['array_floats'] = '[1.34,3,6.42]';
$outputs['array_nulls'] = '[null,null]';
$outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
$outputs['array_list_and_map_difference'] = '{"list":[1,2,3],"map":{"0":1,"2":2,"3":3}}';
$outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
Expand Down Expand Up @@ -82,7 +83,7 @@ protected function getContent($key)
$outputs['virtual_properties_low'] = '{"low":1}';
$outputs['virtual_properties_high'] = '{"high":8}';
$outputs['virtual_properties_all'] = '{"low":1,"high":8}';
$outputs['nullable'] = '{"foo":"bar","baz":null}';
$outputs['nullable'] = '{"foo":"bar","baz":null,"0":null}';
$outputs['null'] = 'null';
$outputs['simple_object_nullable'] = '{"foo":"foo","moo":"bar","camel_case":"boo","null_property":null}';
$outputs['input'] = '{"attributes":{"type":"text","name":"firstname","value":"Adrien"}}';
Expand Down
5 changes: 5 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/xml/array_nulls.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<entry xsi:nil="true"/>
<entry xsi:nil="true"/>
</result>
2 changes: 2 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/yml/array_nulls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- null
- null
1 change: 1 addition & 0 deletions tests/JMS/Serializer/Tests/Serializer/yml/nullable.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
foo: bar
baz: null
0: null

0 comments on commit 0cef8ba

Please sign in to comment.