Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serializing nulls in arrays #626

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/JMS/Serializer/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ abstract class Context
/** @var boolean|null */
private $serializeNull;

/** @var boolean */
private $serializeInArrayNull = false;

private $initialized = false;

/** @var \SplStack */
Expand Down Expand Up @@ -210,6 +213,35 @@ public function shouldSerializeNull()
return $this->serializeNull;
}

/**
* Set if NULLs in arrays should be presented (TRUE) or skipped (FALSE)
*
* @param bool $bool
* @return $this
*/
public function setSerializeInArrayNull($bool)
{
$this->serializeInArrayNull = (boolean) $bool;

return $this;
}

/**
* Returns TRUE when NULLs in arrays should be presented
* Returns FALSE when NULLs in arrays should be skipped
*
* @return bool
*/
public function shouldSerializeInArrayNull()
{
return $this->serializeInArrayNull;
}

public function shouldSerializeNullForKey($key)
{
return is_string($key) ? $this->shouldSerializeNull() : $this->shouldSerializeInArrayNull();
}

/**
* @return string
*/
Expand Down
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() !== true)) {
if (null === $v && ! $context->shouldSerializeNullForKey($k)) {
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() !== true)) {
if (null === $v && ! $context->shouldSerializeNullForKey($k)) {
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 @@ -274,6 +274,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()->setSerializeInArrayNull(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
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