Custom Handler on JMSSerializerBundle is ignored
Asked Answered
C

2

8

I am attempting to use a custom handler for JMS Serializer Bundle

class CustomHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'integer',
                'method' => 'serializeIntToJson',
            ),
        );
    }

    public function serializeIntToJson(JsonSerializationVisitor $visitor, $int, array $type, Context $context)
    {
         die("GIVE ME SOMETHING");
    }
}

This does nothing, and does not die. This is how I am registering the handler

$serializer = SerializerBuilder::create()
    ->configureHandlers(function(HandlerRegistry $registry) {
        $registry->registerSubscribingHandler(new MyHandler());
    })
    ->addDefaultHandlers()
    ->build();

$json = $serializer->serialize($obj, 'json');

My handler is never called and I cannot manipulate the data on serialisation.

Cantatrice answered 15/12, 2014 at 0:33 Comment(4)
Did you ever figure this out? I am having the same problem. getSubscribingMethods() seems to be called but not serializeIntToJsonLogan
@Logan checkout my new answer - although I have answered a different question! Bear with me....Cantatrice
@Chris, OK I have posted another answer now. I hope that helps.Cantatrice
Possible duplicate of Creating a JMS Serializer handler in symfony2Weylin
C
1

I have this which works

    $serializer = SerializerBuilder::create()
        ->configureListeners(function(EventDispatcher $dispatcher) {
            $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
            $dispatcher->addSubscriber(new UserSubscriber($this->container));
        })
        ->addDefaultListeners()
        ->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')
        ->build();

    return $serializer->serialize($project, 'json');

$project is my entity.

You can omit this line if you don't have serializer configs

->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')

I think my main issue was this ->addDefaultListeners().

In config.yml I have

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            NameOfBundle:
                namespace_prefix: ""
                path: "@JakeNameOfBundle/Resources/config/serializer"

I don't have anthing set up to make JMS a service.

Cantatrice answered 9/7, 2015 at 9:47 Comment(0)
H
5

You need to create a service for this handler:

custom_jms_handler:
    class: MyBundle\Serializer\CustomHandler
    tags:
        - { name: jms_serializer.subscribing_handler }

Then make sure you use the registered JMS serializer service

$json = $this->get('jms_serializer')->serialize($obj, 'json');
Hills answered 15/12, 2014 at 1:39 Comment(1)
I had to prefix jms_serializer to the custom_jms_handler when creating the service in services.yml. Like this: jms_serializer.custom_jms_handlerAdytum
C
1

I have this which works

    $serializer = SerializerBuilder::create()
        ->configureListeners(function(EventDispatcher $dispatcher) {
            $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
            $dispatcher->addSubscriber(new UserSubscriber($this->container));
        })
        ->addDefaultListeners()
        ->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')
        ->build();

    return $serializer->serialize($project, 'json');

$project is my entity.

You can omit this line if you don't have serializer configs

->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')

I think my main issue was this ->addDefaultListeners().

In config.yml I have

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            NameOfBundle:
                namespace_prefix: ""
                path: "@JakeNameOfBundle/Resources/config/serializer"

I don't have anthing set up to make JMS a service.

Cantatrice answered 9/7, 2015 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.