Documentation » Core

9. Api »

« 7. Doctrine base entity manager

8. Serialization

8.1. Custom handlers

The bundle comes with a BaseSerializerHandler to let you customize your serialized entities; this handler is used to serialize/deserialize an entity to/from its id within the defaults formats (‘json’, ‘xml’, ‘yml’).

The serializer default formats are configurable. You can change them from the configuration file.

# config/packages/sonata_core.yaml

        formats: ['json', 'xml', 'yml']

You can set these formats to a different array or you can add another format to these formats by using BaseSerializerHandler methods setFormats and addFormat

You are free to create your own handler for your specific needs.

Just override Sonata\CoreBundle\Serializer\BaseSerializerHandler to create a JMS Serializer handler.

You can define your handler like this:

<!-- config/services.xml -->

<service id="" class="App\Serializer\PostSerializerHandler">
    <argument type="service" id=""/>
    <tag name="jms_serializer.subscribing_handler"/>

To call your handler, you can use a custom type used by JMS Serializer, like this:

<property name="post" serialized-name="entity_id" type="post_type"/>

And your handler need to specify the type name:

// src/Serializer/PostSerializerHandler.php

namespace App\Serializer;

use Sonata\CoreBundle\Serializer\BaseSerializerHandler;

class PostSerializerHandler extends BaseSerializerHandler
    public static function getType()
        return 'post_type';

Found a typo? Something is wrong in this documentation? Just fork and edit it!