Skip to content
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

Deserialize xmlKeyValuePairs #840

Merged
merged 4 commits into from
Feb 3, 2018
Merged
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
9 changes: 9 additions & 0 deletions src/JMS/Serializer/GraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public function accept($data, array $type = null, Context $context)
return $visitor->visitDouble($data, $type, $context);

case 'array':
$metadata = $context->getMetadataStack()->count() ? $context->getMetadataStack()->top() : null;
if ($visitor instanceof XmlDeserializationVisitor && null !== $metadata && $metadata->xmlKeyValuePairs) {
$visitor->setCurrentMetadata($metadata);
$result = $visitor->visitArray($data, $type, $context);
$visitor->revertCurrentMetadata();

return $result;
}

return $visitor->visitArray($data, $type, $context);

case 'resource':
Expand Down
17 changes: 17 additions & 0 deletions src/JMS/Serializer/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ public function visitDouble($data, array $type, Context $context)

public function visitArray($data, array $type, Context $context)
{
// handle key-value-pairs
if (null !== $this->currentMetadata && $this->currentMetadata->xmlKeyValuePairs) {
if (2 !== count($type['params'])) {
throw new RuntimeException('The array type must be specified as "array<K,V>" for Key-Value-Pairs.');
}

list($keyType, $entryType) = $type['params'];

$result = [];
foreach ($data as $key => $v) {
$k = $this->navigator->accept($key, $keyType, $context);
$result[$k] = $this->navigator->accept($v, $entryType, $context);
}

return $result;
}

$entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry';
$namespace = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryNamespace ? $this->currentMetadata->xmlEntryNamespace : null;

Expand Down
68 changes: 68 additions & 0 deletions tests/Fixtures/ObjectWithXmlKeyValuePairsWithType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* Copyright 2016 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlKeyValuePairs;

class ObjectWithXmlKeyValuePairsWithType
{
/**
* @var array
* @Type("array<string,string>")
* @XmlKeyValuePairs
*/
private $list;

/**
* @var array
* @Type("array<string>")
*/
private $list2;

public function __construct(array $list, array $list2 = [])
{
$this->list = $list;
$this->list2 = $list2;
}

public static function create1()
{
return new self(
[
'key-one' => 'foo',
'key-two' => 'bar',
]
);
}

public static function create2()
{
return new self(
[
'key_01' => 'One',
'key_02' => 'Two',
'key_03' => 'Three',
],
[
'Four',
]
);
}
}
14 changes: 14 additions & 0 deletions tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use JMS\Serializer\Tests\Fixtures\ObjectWithToString;
use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualXmlProperties;
use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairs;
use JMS\Serializer\Tests\Fixtures\ObjectWithXmlKeyValuePairsWithType;
use JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespaces;
use JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespacesAndObjectProperty;
use JMS\Serializer\Tests\Fixtures\ObjectWithXmlNamespacesAndObjectPropertyAuthor;
Expand Down Expand Up @@ -249,6 +250,19 @@ public function testArrayKeyValues()
$this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), 'xml'));
}

public function testDeserializeArrayKeyValues()
{
$xml = $this->getContent('array_key_values_with_type_1');
$result = $this->serializer->deserialize($xml, ObjectWithXmlKeyValuePairsWithType::class, 'xml');
$this->assertInstanceOf(ObjectWithXmlKeyValuePairsWithType::class, $result);
$this->assertEquals(ObjectWithXmlKeyValuePairsWithType::create1(), $result);

$xml2 = $this->getContent('array_key_values_with_type_2');
$result2 = $this->serializer->deserialize($xml2, ObjectWithXmlKeyValuePairsWithType::class, 'xml');
$this->assertInstanceOf(ObjectWithXmlKeyValuePairsWithType::class, $result2);
$this->assertEquals(ObjectWithXmlKeyValuePairsWithType::create2(), $result2);
}

/**
* @dataProvider getDateTime
* @group datetime
Expand Down
8 changes: 8 additions & 0 deletions tests/Serializer/xml/array_key_values_with_type_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<list>
<key-one><![CDATA[foo]]></key-one>
<key-two><![CDATA[bar]]></key-two>
</list>
<list2></list2>
</result>
11 changes: 11 additions & 0 deletions tests/Serializer/xml/array_key_values_with_type_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<list>
<key_01><![CDATA[One]]></key_01>
<key_02><![CDATA[Two]]></key_02>
<key_03><![CDATA[Three]]></key_03>
</list>
<list2>
<entry><![CDATA[Four]]></entry>
</list2>
</result>