Skip to content

Commit

Permalink
add nullable option for json visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattw authored and hashnz committed Sep 21, 2012
1 parent 4880923 commit 8cb1e3c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private function addVisitorsSection(NodeBuilder $builder)
->arrayNode('json')
->addDefaultsIfNotSet()
->children()
->booleanNode('serialize_null')->defaultFalse()->end()
->scalarNode('options')
->defaultValue(0)
->beforeNormalization()
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public function loadInternal(array $config, ContainerBuilder $container)
$container
->setParameter('jms_serializer.json_serialization_visitor.options', $config['visitors']['json']['options'])
;

$container
->setParameter('jms_serializer.json_serialization_visitor.serialize_null', $config['visitors']['json']['serialize_null'])
;
}

public function getConfiguration(array $config, ContainerBuilder $container)
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<call method="setOptions">
<argument>%jms_serializer.json_serialization_visitor.options%</argument>
</call>
<call method="setNullable">
<argument>%jms_serializer.json_serialization_visitor.serialize_null%</argument>
</call>
<tag name="jms_serializer.serialization_visitor" format="json" />
</service>
<service id="jms_serializer.json_deserialization_visitor" class="%jms_serializer.json_deserialization_visitor.class%" public="false">
Expand Down
4 changes: 2 additions & 2 deletions Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function visitArray($data, $type)
foreach ($data as $k => $v) {
$v = $this->navigator->accept($v, null, $this);

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

Expand Down Expand Up @@ -133,7 +133,7 @@ public function visitProperty(PropertyMetadata $metadata, $data)
: $data->{$metadata->getter}());

$v = $this->navigator->accept($v, null, $this);
if (null === $v) {
if (null === $v && !$this->isNullable()) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions Serializer/JsonSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class JsonSerializationVisitor extends GenericSerializationVisitor
{
private $options = 0;
private $nullable = false;

public function getResult()
{
Expand All @@ -36,4 +37,14 @@ public function setOptions($options)
{
$this->options = (integer) $options;
}

public function setNullable($allowNull)
{
$this->nullable = (bool) $allowNull;
}

public function isNullable()
{
return (bool) $this->nullable;
}
}
10 changes: 10 additions & 0 deletions Tests/Fixtures/ObjectWithNullProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace JMS\SerializerBundle\Tests\Fixtures;

use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;

class ObjectWithNullProperty extends SimpleObject
{
private $nullProperty = null;
}
26 changes: 25 additions & 1 deletion Tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use JMS\SerializerBundle\Tests\Fixtures\Order;
use JMS\SerializerBundle\Tests\Fixtures\Price;
use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
use JMS\SerializerBundle\Tests\Fixtures\ObjectWithNullProperty;
use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
use Metadata\MetadataFactory;
use Symfony\Component\Form\Form;
Expand All @@ -70,6 +71,24 @@

abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
{
public function testNullableArray()
{
if ($this->getFormat() !== 'json') {
$this->markTestSkipped('nullable currently only supported by JSON');
}
$arr = array('foo' => 'bar', 'baz' => null, null);
$this->assertEquals($this->getContent('nullable'), $this->getSerializer(true)->serialize($arr, $this->getFormat()));
}

public function testNullableObject()
{
if ($this->getFormat() !== 'json') {
$this->markTestSkipped('nullable currently only supported by JSON');
}
$obj = new ObjectWithNullProperty('foo', 'bar');
$this->assertEquals($this->getContent('simple_object_nullable'), $this->getSerializer(true)->serialize($obj, $this->getFormat()));
}

public function testString()
{
$this->assertEquals($this->getContent('string'), $this->serialize('foo'));
Expand Down Expand Up @@ -476,7 +495,7 @@ protected function deserialize($content, $type)
return $this->getSerializer()->deserialize($content, $type, $this->getFormat());
}

protected function getSerializer()
protected function getSerializer($serialize_null = false)
{
$factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
$namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
Expand All @@ -490,6 +509,11 @@ protected function getSerializer()
'xml' => new XmlSerializationVisitor($namingStrategy, $customSerializationHandlers),
'yml' => new YamlSerializationVisitor($namingStrategy, $customSerializationHandlers),
);

if ($serialize_null) {
$serializationVisitors['json']->setNullable(true);
}

$deserializationVisitors = array(
'json' => new JsonDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
'xml' => new XmlDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
Expand Down
2 changes: 2 additions & 0 deletions Tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ 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['simple_object_nullable'] = '{"foo":"foo","moo":"bar","camel_case":"boo","null_property":null}';
}

if (!isset($outputs[$key])) {
Expand Down

0 comments on commit 8cb1e3c

Please sign in to comment.