diff --git a/src/JsonSerializationVisitor.php b/src/JsonSerializationVisitor.php index c5b729acd..dcd345508 100644 --- a/src/JsonSerializationVisitor.php +++ b/src/JsonSerializationVisitor.php @@ -167,8 +167,8 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex $k = $this->namingStrategy->translateName($metadata); if ($metadata->inline) { - if (is_array($v)) { - $this->data = array_merge($this->data, $v); + if (is_array($v) || ($v instanceof \ArrayObject)) { + $this->data = array_merge($this->data, (array) $v); } } else { $this->data[$k] = $v; diff --git a/tests/JMS/Serializer/Tests/Fixtures/ObjectWithInlineArray.php b/tests/JMS/Serializer/Tests/Fixtures/ObjectWithInlineArray.php new file mode 100644 index 000000000..866ebae39 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/ObjectWithInlineArray.php @@ -0,0 +1,24 @@ +") + */ + public $array; + + /** + * @param array $array + */ + public function __construct(array $array) + { + $this->array = $array; + } + + +} diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index 59f891339..83c96c4b6 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -27,6 +27,7 @@ use JMS\Serializer\Tests\Fixtures\Author; use JMS\Serializer\Tests\Fixtures\AuthorList; use JMS\Serializer\Tests\Fixtures\ObjectWithEmptyArrayAndHash; +use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray; use JMS\Serializer\Tests\Fixtures\Tag; use JMS\Serializer\VisitorInterface; @@ -280,6 +281,13 @@ public function testSerializeArrayWithEmptyObject() $this->assertEquals('[{}]', $this->serialize(array(new \stdClass()))); } + public function testInlineArray() + { + $object = new ObjectWithInlineArray(['a' => 'b', 'c' => 'd']); + + $this->assertEquals('{"a":"b","c":"d"}', $this->serialize($object)); + } + public function testSerializeRootArrayWithDefinedKeys() { $author1 = new Author("Jim");