Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:stack_overflow:35478874

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
pele_mele:stack_exchange:stack_overflow:35478874 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1pele_mele:stack_exchange:stack_overflow:35478874 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:stack_overflow:stackoverflow-35478874 à pele_mele:stack_exchange:stack_overflow:35478874 alexis
Ligne 1: Ligne 1:
 +====== How to choose the driver to serialize/deserialize with jmsserializer ======
 +I have an object which I serialize using annotations. It works fine.
 +
 +If I am using yaml configuration, it works fine also.
 +
 +My problem is I want to use both in different context. Lets say that in controller one, I want to use the annotation configuration and in controller two, I want to use the yaml configuration. I want to do that because I need to have different field names in those outputs.
 +
 +  * I tried to override the serializer with a new instance using only the annotation.
 +  * I change jmsserializer service configuration to not use specific drivers. It worked but I cannot choose which one to activate dynamically.
 +  * I tried to select the driver in the container but I couldn't make it work.
 +
 +Is this possible? Am I missing something?
 +
 +<WRAP help>
 +I don't see how you can achieve this.
 +
 +But if you to want expose a property differently, you can create different views of your object by using an exclusion strategy.
 +
 +Example :
 +
 +<code php>
 +/**
 + * @JMS\ExclusionPolicy("all")
 + * @ORM\Entity
 + */
 +class FooBar
 +{    
 +    /**
 +     * @ORM\Column(type="string")
 +     * @JMS\Groups({"foo"})
 +     */
 +    protected $name; // output 'name'
 +
 +    /**
 +     * @ORM\Column(type="string")
 +     * @JMS\SerializedName("foo_bar_name")
 +     * @JMS\Accessor(getter="getName", setter="setName")
 +     * @JMS\Groups({"bar"})
 +     */
 +    protected $fooName; // output 'foo_bar_name'
 +
 +    // ...
 +
 +    public function setName($name)
 +    {
 +        $this->address = $name;
 +
 +        return $this;
 +    }
 +
 +    public function getName()
 +    {
 +        return $this->name;
 +    }
 +}
 +</code>
 +
 +Like this, the property can be serialised in two different names :
 +
 +<code php>
 +use JMS\Serializer\SerializationContext;
 +
 +$serializer->serialize(new FooBar(), 'json', SerializationContext::create()->setGroups(array('foo')));
 +
 +//will output $name as 'name'
 +
 +$serializer->serialize(new FooBar(), 'json', SerializationContext::create()->setGroups(array('bar')));
 +
 +//will output $fooName as 'foo_bar_name'
 +</code>
 +
 +Note the ''@JMS\SerializedName'' is not mandatory, you can use it for custom names.
 +
 +See more at the [[http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies|Exclusion strategies]] part of the documentation.
 +
 +Hope this could be an alternative for you.
 +</WRAP>
 +<WRAP info>
 +[[https://stackoverflow.com/questions/35478874/how-to-choose-the-driver-to-serialize-deserialize-with-jmsserializer|php - How to choose the driver to serialize/deserialize with jmsserializer - Stack Overflow]]
 +</WRAP>