2

I tried to follow this answer: Add extra fields using JMS Serializer bundle

but no change..

I want to add extra fields to a serialized entity (in json) before sending it. Is there something that I missed ?

Here is my Listener:

<?php

namespace My\MyBundle\Listener;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use My\MyBundle\Entity\Dossier;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\JsonSerializationVisitor;

/**
 * Add data after serialization
 *
 * @Service("my.listener.serializationlistener")
 * @Tag("jms_serializer.event_subscriber")
 */
class SerializationListener implements EventSubscriberInterface
{

    /**
     * @inheritdoc
     */
    static public function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.post_serialize', 'class' => 'My\MyBundle\Entity\Dossier', 'method' => 'onPostSerialize'),
        );
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        $event->getVisitor()->addData('someKey','someValue');
    }
}

and the call in my controller:

$serializer = $this->container->get('jms_serializer');
$res = $serializer->serialize($dossier, 'json');

I also add the following service declaration:

services:
    my.mybundle.listener:
        class: My\MyBundle\Listener\SerializationListener

I have another service declared and when I change its declaration name symfony give and error, not when I do it with the listener service.

Thanks in advance

Community
  • 1
  • 1
Mehdi
  • 21
  • 4
  • Can you post your actual code ? – Max Apr 02 '15 at 15:28
  • I just added the code – Mehdi Apr 02 '15 at 15:40
  • @Mehdi did you manage to serialze an object as the extra field (instead of the string 'someValue')? Please see my other question: https://stackoverflow.com/questions/45441456/jms-virtualproperty-with-argument-and-custom-listener-subscriber – StockBreak Aug 02 '17 at 12:37

2 Answers2

2
$visitor = $event->getVisitor();
$visitor->visitProperty(new StaticPropertyMetadata('', 'some_key', null),'some_key');

The addData method is for old version of JMS Serializer. Don't forget to import StaticPropertyMetadata.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

Perhaps, you forgot to add a tag. Your listener declaration should looks something like this

services:
    my.bundle.listener:
        class: My\MyBundle\Listener\SerializationListener
        tags:
            -  { name: jms_serializer.event_subscriber }
  • Just in case.. I've just added a new event in my listener (post_serialize). I already have pre_serialize and nothing happens for post event.. Clearing the cache did the trick.. – Delphine Apr 21 '16 at 09:24