Skip to content
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
31 changes: 31 additions & 0 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ final public function deserialize($data, $type, $format, array $context = array(
return $this->denormalize($data, $type, $format, $context);
}

/**
* Deserializes data into an array of the given type.
*
* @param mixed $data
* @param string $type
* @param string $format
* @param array $context
*
* @return object
*
* @throws UnexpectedValueException
*/
public function deserializeCollection($data, $type, $format, array $context = array())
{
if (!$this->supportsDecoding($format)) {
throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
}

$data = $this->decode($data, $format, $context);

if (!is_array($data)) {
throw new UnexpectedValueException(sprintf('Could not deserialize value into array: %s.', $data));
}

foreach ($data as $key => $val) {
$data[$key] = $this->denormalizeObject($datum, $type, $format, $context);
}

return $data;
}

/**
* {@inheritdoc}
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ public function testDeserialize()
$this->assertEquals($data, $result->toArray());
}

public function testDeserializeCollection()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = array(array('title' => 'foo', 'numbers' => array(5, 3)), array('title' => 'bar', 'numbers' => array(2, 8)));
$result = $this->serializer->deserializeCollection(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
$this->assertContainsOnlyInstancesOf('\Symfony\Component\Serializer\Tests\Model', $result);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDeserializeInvalidCollection()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$this->serializer->deserializeCollection('foo', '\Symfony\Component\Serializer\Tests\Model', 'json');
}

public function testDeserializeUseCache()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
Expand Down