Creating a JMS Serializer handler in symfony2
Asked Answered
P

1

8

I tried to follow the related documentation, here:
http://jmsyst.com/libs/serializer/master/configuration
here
http://jmsyst.com/libs/serializer/master/handlers
and here
http://jmsyst.com/bundles/JMSSerializerBundle/master/cookbook/custom_handlers

I'm stuck when trying to get the builder object in symfony2 (see second link for this object). The service is a serializer which has already been build. Where should i instanciate my own handler?

Update:
I'm investigating one possible solution:
$builder = \JMS\Serializer\SerializerBuilder::create();
or
$builder = new \JMS\Serializer\SerializerBuilder();
maybe this will work but it would be nice to let the already instanciated serializer make use of my handler. To get the serializer service in the controller: $serializer = $this->get('jms.serializer');

Persistent answered 14/8, 2013 at 19:40 Comment(0)
P
15

The solution is to create a service with the right tag.

datetimezone_handler:
    class:     MyVendor\MyBundle\Serializer\Handler\DateTimeZoneHandler
    tags:
        - { name: jms_serializer.subscribing_handler }
Persistent answered 15/3, 2014 at 10:4 Comment(2)
you save my day twice in this month. I used to serialize FosUser entity, for entity "type" need by "My\\UserBundle\\Entity\\User"Jung
This doesn't work for me. I have it set up like this and it still doesn't register the handler. It never gets into getSubscribingMethods() on the handler.Yoko

© 2022 - 2024 — McMap. All rights reserved.