Skip to content

Commit

Permalink
is_scalar(null) !== true
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 authored and fabpot committed Feb 1, 2011
1 parent 2889e91 commit 46d9006
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Expand Up @@ -25,7 +25,7 @@ class JsonEncoder extends AbstractEncoder
*/
public function encode($data, $format)
{
if (!is_scalar($data)) {
if ($this->serializer->isStructuredType($data)) {
$data = $this->serializer->normalize($data, $format);
}
return json_encode($data);
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -37,12 +37,12 @@ public function encode($data, $format)
$this->dom = new \DOMDocument();
$this->format = $format;

if (is_scalar($data)) {
$this->appendNode($this->dom, $data, 'response');
} else {
if ($this->serializer->isStructuredType($data)) {
$root = $this->dom->createElement('response');
$this->dom->appendChild($root);
$this->buildXml($root, $data);
} else {
$this->appendNode($this->dom, $data, 'response');
}
return $this->dom->saveXML();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function buildXml($parentNode, $data)
}
if (is_object($data)) {
$data = $this->serializer->normalizeObject($data, $this->format);
if (is_scalar($data)) {
if (!$this->serializer->isStructuredType($data)) {
// top level data object is normalized into a scalar
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
Expand Down Expand Up @@ -250,4 +250,4 @@ protected function isElementNameValid($name)
{
return $name && strpos($name, ' ') === false && preg_match('|^\w+$|', $name);
}
}
}
Expand Up @@ -52,7 +52,7 @@ public function normalize($object, $format, $properties = null)

if (null === $propertyMap || isset($propertyMap[$attributeName])) {
$attributeValue = $method->invoke($object);
if (!is_scalar($attributeValue)) {
if ($this->serializer->isStructuredType($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format);
}

Expand Down Expand Up @@ -142,4 +142,4 @@ protected function isGetMethod(\ReflectionMethod $method)
0 === $method->getNumberOfRequiredParameters()
);
}
}
}
7 changes: 6 additions & 1 deletion src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -32,6 +32,11 @@ class Serializer implements SerializerInterface
protected $encoders = array();
protected $normalizerCache = array();

public function isStructuredType($val)
{
return null !== $val && !is_scalar($val);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -90,7 +95,7 @@ public function normalize($data, $format)
{
if (is_array($data)) {
foreach ($data as $key => $val) {
$data[$key] = is_scalar($val) ? $val : $this->normalize($val, $format);
$data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
}
return $data;
}
Expand Down

0 comments on commit 46d9006

Please sign in to comment.