将复杂对象图序列化为XML并返回

时间:2015-11-08 10:41:21

标签: php symfony serialization xml-serialization symfony-2.7

问题

我有以下对象图:

AppBundle\Controller\Person Object
(
    [name:AppBundle\Controller\Person:private] => Person Name
    [age:AppBundle\Controller\Person:private] => 10
    [phones:AppBundle\Controller\Person:private] => Array
        (
            [0] => AppBundle\Controller\Telephone Object
                (
                    [phone:AppBundle\Controller\Telephone:private] => 221-16-78
                )
            [1] => AppBundle\Controller\Telephone Object
                (
                    [phone:AppBundle\Controller\Telephone:private] => 221-16-78
                )
        )
)

使用我的控制器中的以下代码进行序列化:

$s = $this->get("serializer")->serialize($person, 'xml');

这给了我以下结果:

<response>
  <name>Person Name</name>
  <age>10</age>
  <phones>
    <phone>221-16-78</phone>
  </phones>
  <phones>
    <phone>221-16-78</phone>
  </phones>
</response>

使用以下代码进行反序列化:

$this->get("serializer")->deserialize($s, 'AppBundle\Controller\Person', 'xml');

提供以下内容:

AppBundle\Controller\Person Object
(
    [name:AppBundle\Controller\Person:private] => Person Name
    [age:AppBundle\Controller\Person:private] => 10
    [phones:AppBundle\Controller\Person:private] => Array
        (
            [0] => Array
                (
                    [phone] => 221-16-78
                )
            [1] => Array
                (
                    [phone] => 221-16-78
                )
        )
)

问题

首先,有没有办法按如下方式接收xml:

<response>
  <name>Person Name</name>
  <age>10</age>
  <phones>
    <telephone>
      <phone>221-16-78</phone>
    </telephone>
    <telephone>
      <phone>221-16-78</phone>
    </telephone>
  </phones>
</response>

第二,如何将其反序列化回对象图(人和电话),一旦使用简单的反序列化,它将返回一个具有一组关联数组而不是Telephone个对象的电话数组的对象。

型号/控制器

这是我的模型/控制器类:

<?php

namespace AppBundle\Controller;

class Person {

    private $name;
    private $age;
    private $phones;

    public function getName() {
        return $this->name;
    }

    public function getAge() {
        return $this->age;
    }

    public function getPhones() {
        return $this->phones;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setAge($age) {
        $this->age = $age;
    }

    public function setPhones($phones) {
        $this->phones = $phones;
    }

}

<?php

namespace AppBundle\Controller;

class Telephone {

    private $phone;

    public function getPhone() {
        return $this->phone;
    }

    public function setPhone($phone) {
        $this->phone = $phone;
    }

}

我的控制器方法:

/**
 * @Route("/", name="homepage")
 */
public function indexAction(Request $request)
{

    $phone1 = new Telephone();
    $phone1->setPhone("221-16-78");

    $phone2 = new Telephone();
    $phone2->setPhone("221-16-78");

    $person = new Person();
    $person->setName("Person Name");
    $person->setAge(10);
    $person->setPhones(array($phone1, $phone2));

    print_r($person);

    $s = $this->get("serializer")->serialize($person, 'xml');
    $p = $this->get("serializer")->deserialize($s, 'AppBundle\Controller\Person', 'xml');

    print_r($p);

}

使用JMS Serializer的解决方案

我能够通过使用注释(基本上是类型和列表详细信息)为序列化程序提供一些提示来解决JMS Serializer的这个问题,如下所示:

<?php

namespace AppBundle\Controller;

use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlList;

class Person {

    /**
     * @Type("string")
     */
    private $name;
    /**
     * @Type("integer")
     */
    private $age;
    /**
     * @Type("array<AppBundle\Controller\Telephone>")
     * @XmlList(entry = "telephone")
     */
    private $phones;

    public function getName() {
        return $this->name;
    }

    public function getAge() {
        return $this->age;
    }

    public function getPhones() {
        return $this->phones;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setAge($age) {
        $this->age = $age;
    }

    public function setPhones($phones) {
        $this->phones = $phones;
    }

}

<?php

namespace AppBundle\Controller;

use JMS\Serializer\Annotation\Type;

class Telephone {

    /**
     * @Type("string")
     */
    private $phone;

    public function getPhone() {
        return $this->phone;
    }

    public function setPhone($phone) {
        $this->phone = $phone;
    }

}

1 个答案:

答案 0 :(得分:2)

根据我的理解,当你$this->get('serializer')时,你会得到Symfony Serialization component。我并不完全熟悉它(你会在勾选中看到原因)但是他们的文档确实强调了handle object properties如何只是为了序列化。

对于您似乎正在寻找的序列化视图的控制类型,我强烈建议JMS serializer(和associated bundle)通过注释或外部对输出进行更细粒度的控制CONFIGS。在你的情况下,做:

use JMS\Serializer\Annotation as Serializer;

class Person
{
    /**
     * @Serializer\Type("array<Telephone>")
     */
    private $phones;

可能足以获得您正在寻找的双向序列化。