diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index 09ec77129a..4aeddabe75 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -33,6 +33,7 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException; * @author Jordi Boggiano * @author Johannes M. Schmitt * @author Lukas Kahwe Smith + * @author Kévin Dunglas */ class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface { @@ -118,6 +119,11 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz */ 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; } @@ -172,21 +178,25 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz /** * 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)) { + $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; } diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index c2a231b11e..3a6b1ae4c5 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -73,6 +73,15 @@ class SerializerTest extends \PHPUnit_Framework_TestCase $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()));