Skip to content

[Serializer] Normalizers can serialize collections and scalars #13500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
20 changes: 15 additions & 5 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
{
Expand Down Expand Up @@ -118,6 +119,11 @@ final public function deserialize($data, $type, $format, array $context = array(
*/
public function normalize($data, $format = null, array $context = array())
{
// If a normalizer supports the given data, use it
if ($normalizer = $this->getNormalizer($data, $format)) {
return $normalizer->normalize($data, $format, $context);
}

if (null === $data || is_scalar($data)) {
return $data;
}
Expand Down Expand Up @@ -172,21 +178,25 @@ public function supportsDenormalization($data, $type, $format = null)
/**
* Returns a matching normalizer.
*
* @param object $data The object to get the serializer for
* @param mixed $data Data to get the serializer for
* @param string $format format name, present to give the option to normalizers to act differently based on formats
*
* @return NormalizerInterface|null
*/
private function getNormalizer($data, $format)
{
$class = get_class($data);
if (isset($this->normalizerCache[$class][$format])) {
return $this->normalizerCache[$class][$format];
if ($isObject = is_object($data)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can pass something else than an object to this method, you need to update the phpdoc

$class = get_class($data);
if (isset($this->normalizerCache[$class][$format])) {
return $this->normalizerCache[$class][$format];
}
}

foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;
if ($isObject) {
$this->normalizerCache[$class][$format] = $normalizer;
}

return $normalizer;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public function testDenormalizeOnNormalizer()
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
}

public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
{
$this->serializer = new Serializer(array(new TestNormalizer()), array());
$this->assertNull($this->serializer->normalize(array('a', 'b')));
$this->assertNull($this->serializer->normalize(new \ArrayObject(array('c', 'd'))));
$this->assertNull($this->serializer->normalize(array()));
$this->assertNull($this->serializer->normalize('test'));
}

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