Skip to content

Commit 6bfebdc

Browse files
committedJul 16, 2014
Merge pull request schmittjoh#302 from goetas/xmlbooleans
Unserialization of XML booleans
2 parents 10de160 + 24f4601 commit 6bfebdc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed
 

Diff for: ‎src/JMS/Serializer/XmlDeserializationVisitor.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public function visitBoolean($data, array $type, Context $context)
104104
{
105105
$data = (string) $data;
106106

107-
if ('true' === $data) {
107+
if ('true' === $data || '1' === $data) {
108108
$data = true;
109-
} elseif ('false' === $data) {
109+
} elseif ('false' === $data || '0' === $data) {
110110
$data = false;
111111
} else {
112-
throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", or "false", but got %s.', json_encode($data)));
112+
throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", "false", "1" or "0", but got %s.', json_encode($data)));
113113
}
114114

115115
if (null === $this->result) {
@@ -224,9 +224,9 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
224224
$nodes = $data->xpath('./@'.$attributeName);
225225
if (!empty($nodes)) {
226226
$v = (string) reset($nodes);
227-
$metadata->reflection->setValue($this->currentObject, $v);
227+
$metadata->reflection->setValue($this->currentObject, $v);
228228
}
229-
229+
230230
} elseif (isset($data[$name])) {
231231
$v = $this->navigator->accept($data[$name], $metadata->type, $context);
232232
$metadata->reflection->setValue($this->currentObject, $v);

Diff for: ‎tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ public function testInvalidUsageOfXmlValue()
4747
$this->serialize($obj);
4848
}
4949

50+
51+
/**
52+
* @dataProvider getXMLBooleans
53+
*/
54+
public function testXMLBooleans($xmlBoolean, $boolean)
55+
{
56+
if ($this->hasDeserializer()) {
57+
$this->assertSame($boolean, $this->deserialize('<result>'.$xmlBoolean.'</result>', 'boolean'));
58+
}
59+
}
60+
61+
public function getXMLBooleans()
62+
{
63+
return array(array('true', true), array('false', false), array('1', true), array('0', false));
64+
}
65+
5066
public function testPropertyIsObjectWithAttributeAndValue()
5167
{
5268
$personCollection = new PersonLocation;

0 commit comments

Comments
 (0)