Bläddra i källkod

Array typed as hashes (array<X,X>), are now correctly serialized when empty.

Adrien Brault 12 år sedan
förälder
incheckning
394b12cd6a

+ 1 - 1
Serializer/GenericSerializationVisitor.php

@@ -84,7 +84,7 @@ abstract class GenericSerializationVisitor extends AbstractVisitor
             $this->root = array();
             $rs = &$this->root;
         } else {
-            $rs = array();
+            $rs = isset($type['params'][1]) ? new \ArrayObject() : array();
         }
 
         foreach ($data as $k => $v) {

+ 2 - 1
Serializer/XmlSerializationVisitor.php

@@ -213,7 +213,8 @@ class XmlSerializationVisitor extends AbstractVisitor
         if ($addEnclosingElement) {
             $this->revertCurrentNode();
 
-            if ($element->hasChildNodes() || $element->hasAttributes()) {
+            if ($element->hasChildNodes() || $element->hasAttributes()
+                || (isset($metadata->type['name']) && $metadata->type['name'] === 'array' && isset($metadata->type['params'][1]))) {
                 $this->currentNode->appendChild($element);
             }
         }

+ 8 - 0
Serializer/YamlSerializationVisitor.php

@@ -72,6 +72,7 @@ class YamlSerializationVisitor extends AbstractVisitor
 
     public function visitArray($data, array $type)
     {
+        $count = $this->writer->changeCount;
         $isList = array_keys($data) === range(0, count($data) - 1);
 
         foreach ($data as $k => $v) {
@@ -96,6 +97,13 @@ class YamlSerializationVisitor extends AbstractVisitor
 
             $this->writer->outdent();
         }
+
+        if ($count === $this->writer->changeCount && isset($type['params'][1])) {
+            $this->writer
+                ->rtrim(false)
+                ->writeln(' {}')
+            ;
+        }
     }
 
     public function visitBoolean($data, array $type)

+ 29 - 0
Tests/Fixtures/ObjectWithEmptyHash.php

@@ -0,0 +1,29 @@
+<?php
+
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\SerializerBundle\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation as Serializer;
+
+class ObjectWithEmptyHash
+{
+    /**
+     * @Serializer\Type("array<string,string>")
+     */
+    private $hash = array();
+}

+ 6 - 0
Tests/Serializer/BaseSerializationTest.php

@@ -70,6 +70,7 @@ use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
 use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
 use JMS\SerializerBundle\Tests\Serializer\Fixture\Article;
 use JMS\SerializerBundle\Tests\Fixtures\Input;
+use JMS\SerializerBundle\Tests\Fixtures\ObjectWithEmptyHash;
 use Metadata\MetadataFactory;
 use Symfony\Component\Form\Form;
 use Symfony\Component\Form\FormError;
@@ -500,6 +501,11 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->getContent('input'), $this->serializer->serialize(new Input(), $this->getFormat()));
     }
 
+    public function testObjectWithEmptyHash()
+    {
+        $this->assertEquals($this->getContent('hash_empty'), $this->serializer->serialize(new ObjectWithEmptyHash(), $this->getFormat()));
+    }
+
     abstract protected function getContent($key);
     abstract protected function getFormat();
 

+ 1 - 0
Tests/Serializer/JsonSerializationTest.php

@@ -81,6 +81,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['virtual_properties_high'] = '{"high":8}';
             $outputs['virtual_properties_all'] = '{"low":1,"high":8}';
             $outputs['input'] = '{"attributes":{"type":"text","name":"firstname","value":"Adrien"}}';
+            $outputs['hash_empty'] = '{"hash":{}}';
         }
 
         if (!isset($outputs[$key])) {

+ 4 - 0
Tests/Serializer/xml/hash_empty.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <hash/>
+</result>

+ 1 - 0
Tests/Serializer/yml/hash_empty.yml

@@ -0,0 +1 @@
+hash: {}