How to convert Serializer annotations to YML or XML configuration?

We’ve been tasked with converting all the serialization annotations in our classes to dedicated configuration classes.

There are a few dozen classes in this application, with at around 20 of them with custom annotations for the serializer (with the rest being serialized without the need of any custom configuration).

Is there a way to produce configuration files (XML preferred, but YAML acceptable) from the existing serializer configuration?

For example, starting from these annotations:

namespace Acme;

use SymfonyComponentSerializerAnnotationGroups;

class MyObj
{
    /**
     * @Groups({"group1", "group2"})
     * @MaxDepth(2)
     */
    public $foo;

    /**
     * @Groups("group3")
     * @SerializedName("bar_name")
     */
    public function getBar() 
    {
        return $this->bar;
    }
// rest of the class

}

Generate this YAML:

AcmeMyObj:
    attributes:
        foo:
            groups: ['group1', 'group2']
            max_depth: 2
        bar:
            groups: ['group3']
            serialized_name: bar_name

Or this XML:

<?xml version="1.0" ?>
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
      https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd">
   <class name="AcmeMyObj">
       <attribute name="foo" max-depth="2">
           <group>group1</group>
           <group>group2</group>
       </attribute>

        <attribute name="bar" serialized-name="bar_name">
           <group>group3</group>
       </attribute>
   </class>
</serializer>

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *