dongyao2129 2016-04-05 17:06
浏览 83
已采纳

具有XML反序列化的JMS序列化程序列表多态

I need a little help, I have next scenario:

AbtsractItem File:

<?php
namespace Com;

use JMS\Serializer\Annotation as JMS;

/**
 * @JMS\Discriminator(
 *      field = "objectType", 
 *      map = {
 *          "part":  "Com\Part",
 *          "complement" : "Com\Complement"
 *          },
 *      disabled=true
 * )
 */
abstract class AbstractItem 
{
    protected $objectType;
}   

Part File:

<?php
namespace Com;

use JMS\Serializer\Annotation as JMS;

class Part extends AbstractItem
{
    /**
     * @JMS\Type("string")
     * @JMS\XmlElement(cdata=false)
     */
    protected $objectType = "Part";
    /**
     * @JMS\Type("string")
     * @JMS\XmlElement(cdata=false)
     */
    private $data;
    //getters & setters
}

Complement File:

<?php
namespace Com;

use JMS\Serializer\Annotation as JMS;

class Complement extends AbstractItem
{
    /**
     * @JMS\Type("string")
     * @JMS\XmlElement(cdata=false)
     */
    protected $objectType = "Complemet";
    /**
     * @JMS\Type("string")
     * @JMS\XmlElement(cdata=false)
     */
    private $number;
    //getters & setters
}

MyObject File:

<?php
namespace Com;

use JMS\Serializer\Annotation as JMS;

/**
 * @JMS\XmlRoot("MyObjects")
 */
class MyObject
{
    /**
     * @JMS\Type("array<Com\AbstractItem>")
     * @JMS\XmlElement(cdata=false)
     * @JMS\XmlList(inline=false, entry="item")    
     */
    private $items;
    //getters & setters
}

So I expected something like the next XML:

<MyObjects>
    <item>
        <objectType>Part</objectType>
        <number>1237173</number>        
    </item>
    <item>
        <objectType>Complement</objectType>
        <data>loremp ipsum...</data>
    </item>
</MyObjects>

when I serialized/deserialized in JSON I have not problem, but not like that for XML. I saw the examples for arrays, for polymorphic attributes and the discriminator, but in my case, I need to have a AbstractItem collection, in this way I'm getting a message "Cannot instantiate abstract class Com\AbtsractItem", if my class it wasn't abstract I just get the attribue objectType in the xml cause is serializing a Item but no the children.

  • 写回答

1条回答 默认 最新

  • doujing6053 2016-04-22 18:07
    关注

    Well, I get it,

    My first problem it was the version, i was using phpDocumentor so was 0.16, when I remove phpDocumentor I could update to 1.*, on the code my changes:

    Note: The object Part and Complement implements the new ItemInterface, removing the abstract class and extends

    Interface File:

    <?php
    namespace Com;
    
    use JMS\Serializer\Annotation as JMS;
    
    /**
     * @JMS\Discriminator(
     *      field = "objectType", 
     *      map = {
     *          "part":  "Com\Part",
     *          "complement" : "Com\Complement"
     *          },
     *      disabled=true
     * )
     */
    interface ItemInterface 
    {
    }   
    

    My Object File:

    <?php
    namespace Com;
    
    use JMS\Serializer\Annotation as JMS;
    
    /**
     * @JMS\XmlRoot("MyObjects")
     */
    class MyObject 
    {
        /**
         * @JMS\Type("array<Com\ItemInterface>")
         * @JMS\XmlElement(cdata=false)
         * @JMS\XmlList(inline=false, entry="item")    
         */
        private $items;
        //getters & setters
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP