I've found the solution by myself,
to add a custom field after the serialization has been done we've to create a listener class like this:
<?php
namespace AcmeDemoBundleListener;
use JMSDiExtraBundleAnnotationService;
use JMSDiExtraBundleAnnotationTag;
use JMSDiExtraBundleAnnotationInject;
use JMSDiExtraBundleAnnotationInjectParams;
use SymfonyComponentHttpKernelEventPostResponseEvent;
use AcmeDemoBundleEntityTeam;
use JMSSerializerHandlerSubscribingHandlerInterface;
use JMSSerializerEventDispatcherEventSubscriberInterface;
use JMSSerializerEventDispatcherPreSerializeEvent;
use JMSSerializerEventDispatcherObjectEvent;
use JMSSerializerGraphNavigator;
use JMSSerializerJsonSerializationVisitor;
/**
* Add data after serialization
*
* @Service("acme.listener.serializationlistener")
* @Tag("jms_serializer.event_subscriber")
*/
class SerializationListener implements EventSubscriberInterface
{
/**
* @inheritdoc
*/
static public function getSubscribedEvents()
{
return array(
array('event' => 'serializer.post_serialize', 'class' => 'AcmeDemoBundleEntityTeam', 'method' => 'onPostSerialize'),
);
}
public function onPostSerialize(ObjectEvent $event)
{
$event->getVisitor()->addData('someKey','someValue');
}
}
That way you can add data to the serialized element.
Instead, if you want to edit an object just before serialization use the pre_serialize event, be aware that you need to already have a variable (and the correct serialization groups) if you want to use pre_serialize for adding a value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…