JMS Serializer Deserialize with abstract parent class
Asked Answered
P

1

8

I have an abstract parent (mapped super-)class which has several children with different properties which I'd like to deserialize. I'm storing the data using MongoDB and Doctrine ODM, so I also have a discriminator field which tells doctrine which subclass is used (and also have a custom "type" property ontop which is used elsewhere to determine which class is currently processed).

When deserializing my model, I get an exception telling me that its impossible to create an instance of an abstract class (ofcourse) - now I'm wondering how I can tell the JMS Deserializer which inherited class it should use (that is why I use a custom type instance variable for example - because I have no access to doctrine's discriminator field mapping).

I can successfully hook into the preDeserializeEvent- so maybe it is possible to make some switch/cases there (or using the )?

My Model in short (abstract class):

<?php 

namespace VBCMS\Bundle\AdminBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\Serializer\Annotation as Serializer;

/**
 * abstract Class Module
 * @Serializer\AccessType("public_method")
 * @MongoDB\MappedSuperclass
 * @MongoDB\InheritanceType("SINGLE_COLLECTION")
 * @MongoDB\DiscriminatorField(fieldName="_discriminator_field")
 * @MongoDB\DiscriminatorMap({
 *    "module"="Module",
 *    "text_module"="TextModule",
 *    "menu_module"="MenuModule",
 *    "image_module"="ImageModule"
 * })
 */
abstract class Module {

  const TYPE_MODULE_TEXT        = 'module.text';
  const TYPE_MODULE_MENU        = 'module.menu';
  const TYPE_MODULE_MEDIA_ITEM  = 'module.media.item';

  /**
   * @Serializer\Type("string")
   * @MongoDB\Field(type="string")
   * @var String
   */
  protected $type;

  /**
   * @Serializer\Type("boolean")
   * @MongoDB\Field(type="boolean")
   * @var boolean
   */
  protected $visible;

  // getter/setter methods etc..
}

?>

One of the subclasses

<?php
namespace VBCMS\Bundle\AdminBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\Serializer\Annotation as Serializer;

/**
 * Class TextModule
 * @package VBCMS\Bundle\AdminBundle\Document
 * @Serializer\AccessType("public_method")
 * @MongoDB\EmbeddedDocument
 */
class TextModule extends Module {

    const TEXT_TYPE_SPLASH_HEADLINE = 'splashscreen.headline';
    const TEXT_TYPE_SPLASH_SUBLINE  = 'splashscreen.subline';

    /**
     * the actual text
     *
     * @var string
     * @Serializer\Type("string")
     * @MongoDB\Field(type="string")
     */
    protected $text;

    /**
     * how it is called in the admin interface
     *
     * @var string
     * @Serializer\Type("string")
     * @MongoDB\Field(type="string")
     */
    protected $label;

    /**
     * @var string
     * @Serializer\Type("string")
     * @MongoDB\Field(type="string")
     */
    protected $textType;

    // getter/setter methods etc..
?>

Another test was to not make the Module class abstract and to create a custom static method

/**
 *
 * @Serializer\HandlerCallback("json", direction="deserialization")
 * @param JsonDeserializationVisitor $visitor
 */
public static function deserializeToObject(JsonDeserializationVisitor $visitor)
{
  // modify visitor somehow to return an instance of the desired inherited module class??
}

any ideas?

Patter answered 24/6, 2013 at 12:8 Comment(1)
you should copy your "update" as an anwser and mark this question anwsered - this way other who might have the same problem will clearly see that there is a working solution on this pageYaekoyael
P
8

I found a discriminator mapping in the Tests directory of the plugin, unfortunately, this is not yet documented: https://github.com/schmittjoh/serializer/blob/master/tests/JMS/Serializer/Tests/Fixtures/Discriminator/Vehicle.php

Documentation is updated http://jmsyst.com/libs/serializer/master/reference/annotations#discriminator

namespace JMS\Serializer\Tests\Fixtures\Discriminator;

use JMS\Serializer\Annotation as Serializer;

/**
 * @Serializer\Discriminator(field = "type", map = {
 *    "car": "JMS\Serializer\Tests\Fixtures\Discriminator\Car",
 *    "moped": "JMS\Serializer\Tests\Fixtures\Discriminator\Moped",
 * })
 */
abstract class Vehicle
{
    /** @Serializer\Type("integer") */
    public $km;

    public function __construct($km)
    {
        $this->km = (integer) $km;
    }
}
Patter answered 28/2, 2014 at 19:0 Comment(1)
Trying to use this for deserialization I get LogicError: "The discriminator field name "..." for base-class "..." was not found in input data". But I don't want the input data to have this field. The field is only to overcome PHP's "type system".Gramarye

© 2022 - 2024 — McMap. All rights reserved.