From 4104c7b0739ab9f8257fd53077499d4044560f06 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 8 May 2011 18:14:44 +0200 Subject: [PATCH] [Serializer] Inlined back the logic from isStructuredType and removed the method --- .../Serializer/Encoder/JsonEncoder.php | 3 --- .../Serializer/Encoder/XmlEncoder.php | 20 +++++++++---------- .../Normalizer/GetSetMethodNormalizer.php | 2 +- .../Component/Serializer/Serializer.php | 11 +--------- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php index 5ab6876430..d98232ee36 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php @@ -25,9 +25,6 @@ class JsonEncoder extends AbstractEncoder implements DecoderInterface */ public function encode($data, $format) { - if ($this->serializer->isStructuredType($data)) { - $data = $this->serializer->normalize($data, $format); - } return json_encode($data); } diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index 2e6dd1df8c..fc2f5bb725 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -38,7 +38,7 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface $this->dom = new \DOMDocument(); $this->format = $format; - if ($this->serializer->isStructuredType($data)) { + if (null !== $data && !is_scalar($data)) { $root = $this->dom->createElement($this->rootNodeName); $this->dom->appendChild($root); $this->buildXml($root, $data); @@ -248,16 +248,16 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface } if (is_object($data)) { $data = $this->serializer->normalizeObject($data, $this->format); - if (!$this->serializer->isStructuredType($data)) { - // top level data object is normalized into a scalar - if (!$parentNode->parentNode->parentNode) { - $root = $parentNode->parentNode; - $root->removeChild($parentNode); - return $this->appendNode($root, $data, $this->rootNodeName); - } - return $this->appendNode($parentNode, $data, 'data'); + if (null !== $data && !is_scalar($data)) { + return $this->buildXml($parentNode, $data); } - return $this->buildXml($parentNode, $data); + // top level data object was normalized into a scalar + if (!$parentNode->parentNode->parentNode) { + $root = $parentNode->parentNode; + $root->removeChild($parentNode); + return $this->appendNode($root, $data, $this->rootNodeName); + } + return $this->appendNode($parentNode, $data, 'data'); } throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true)); } diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index 76f5e397a4..9dc8e7f0fc 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -52,7 +52,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer if (null === $propertyMap || isset($propertyMap[$attributeName])) { $attributeValue = $method->invoke($object); - if ($this->serializer->isStructuredType($attributeValue)) { + if (null !== $attributeValue && !is_scalar($attributeValue)) { $attributeValue = $this->serializer->normalize($attributeValue, $format); } diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index fadd5f35a7..7cb554fcc4 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -34,15 +34,6 @@ class Serializer implements SerializerInterface protected $normalizerCache = array(); protected $denormalizerCache = array(); - /** - * @param mixed $value value to test - * @return Boolean whether the type is a structured type (array + objects) - */ - public function isStructuredType($value) - { - return null !== $value && !is_scalar($value); - } - /** * {@inheritdoc} */ @@ -104,7 +95,7 @@ class Serializer implements SerializerInterface */ public function normalize($data, $format = null) { - if (!$this->isStructuredType($data)) { + if (null === $value || is_scalar($value)) { return $data; } if ($data instanceof Traversable) {