Skip to content

Commit

Permalink
makes sure that explicit lists are serialized as lists
Browse files Browse the repository at this point in the history
If you want to make sure something is formatted as a list, i.e. ``[1, 2, 3]`` in JSON,
make sure to add an explicit type like ``@Type("array<integer>")``.

closes #571
closes #572
closes #488
closes #304

...and probably more
  • Loading branch information
schmittjoh committed May 21, 2016
1 parent 602bd84 commit b0a46c8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@ public function visitArray($data, array $type, Context $context)
$rs = array();
}

$isList = isset($type['params'][0]) && ! isset($type['params'][1]);

foreach ($data as $k => $v) {
$v = $this->navigator->accept($v, $this->getElementType($type), $context);

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

$rs[$k] = $v;
if ($isList) {
$rs[] = $v;
} else {
$rs[$k] = $v;
}
}

return $rs;
Expand Down
3 changes: 2 additions & 1 deletion src/JMS/Serializer/YamlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public function visitString($data, array $type, Context $context)
public function visitArray($data, array $type, Context $context)
{
$count = $this->writer->changeCount;
$isList = array_keys($data) === range(0, count($data) - 1);
$isList = (isset($type['params'][0]) && ! isset($type['params'][1]))
|| array_keys($data) === range(0, count($data) - 1);

foreach ($data as $k => $v) {
if (null === $v && ( ! is_string($k) || ! $context->shouldSerializeNull())) {
Expand Down
20 changes: 20 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/ObjectWithIntListAndIntMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

class ObjectWithIntListAndIntMap
{
/** @Serializer\Type("array<integer>") @Serializer\XmlList */
private $list;

/** @Serializer\Type("array<integer,integer>") @Serializer\XmlMap */
private $map;

public function __construct(array $list, array $map)
{
$this->list = $list;
$this->map = $map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use JMS\Serializer\Tests\Fixtures\Garage;
use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
use JMS\Serializer\Tests\Fixtures\ObjectWithIntListAndIntMap;
use JMS\Serializer\Tests\Fixtures\Tag;
use JMS\Serializer\Tests\Fixtures\Timestamp;
use JMS\Serializer\Tests\Fixtures\Tree;
Expand Down Expand Up @@ -277,6 +278,13 @@ public function testArrayObjects()
}
}

public function testArrayListAndMapDifference()
{
$arrayData = array(0 => 1, 2 => 2, 3 => 3); // Misses key 1
$data = new ObjectWithIntListAndIntMap($arrayData, $arrayData);

$this->assertEquals($this->getContent('array_list_and_map_difference'), $this->serialize($data));
}

public function testDateTimeArrays()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected function getContent($key)
$outputs['array_empty'] = '{"array":[]}';
$outputs['array_floats'] = '[1.34,3,6.42]';
$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]]';
$outputs['array_datetimes_object'] = '{"array_with_default_date_time":["2047-01-01T12:47:47+0000","2013-12-05T00:00:00+0000"],"array_with_formatted_date_time":["01.01.2047 12:47:47","05.12.2013 00:00:00"]}';
$outputs['array_named_datetimes_object'] = '{"named_array_with_formatted_date":{"testdate1":"01.01.2047 12:47:47","testdate2":"05.12.2013 00:00:00"}}';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<list>
<entry>1</entry>
<entry>2</entry>
<entry>3</entry>
</list>
<map>
<entry _key="0">1</entry>
<entry _key="2">2</entry>
<entry _key="3">3</entry>
</map>
</result>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
list:
- 1
- 2
- 3
map:
0: 1
2: 2
3: 3

0 comments on commit b0a46c8

Please # to comment.