1Event System 2============ 3 4The serializer dispatches different events during the serialization, and 5deserialization process which you can use to hook in and alter the default 6behavior. 7 8Register an Event Listener, or Subscriber 9----------------------------------------- 10The difference between listeners, and subscribers is that listener do not know to which events they listen 11while subscribers contain that information. Thus, subscribers are easier to share, and re-use. Listeners 12on the other hand, can be simple callables and do not require a dedicated class. 13 14.. code-block :: php 15 16 class MyEventSubscriber implements JMS\Serializer\EventDispatcher\EventSubscriberInterface 17 { 18 public static function getSubscribedEvents() 19 { 20 return array( 21 array( 22 'event' => 'serializer.pre_serialize', 23 'method' => 'onPreSerialize', 24 'class' => 'AppBundle\\Entity\\SpecificClass', // if no class, subscribe to every serialization 25 'format' => 'json', // optional format 26 'priority' => 0, // optional priority 27 ), 28 ); 29 } 30 31 public function onPreSerialize(JMS\Serializer\EventDispatcher\PreSerializeEvent $event) 32 { 33 // do something 34 } 35 } 36 37 $builder 38 ->configureListeners(function(JMS\Serializer\EventDispatcher\EventDispatcher $dispatcher) { 39 $dispatcher->addListener('serializer.pre_serialize', 40 function(JMS\Serializer\EventDispatcher\PreSerializeEvent $event) { 41 // do something 42 } 43 ); 44 45 $dispatcher->addSubscriber(new MyEventSubscriber()); 46 }) 47 ; 48 49Events 50------ 51 52serializer.pre_serialize 53~~~~~~~~~~~~~~~~~~~~~~~~ 54This is dispatched before a type is visited. You have access to the visitor, 55data, and type. Listeners may modify the type that is being used for 56serialization. 57 58**Event Object**: ``JMS\Serializer\EventDispatcher\PreSerializeEvent`` 59 60serializer.post_serialize 61~~~~~~~~~~~~~~~~~~~~~~~~~ 62This is dispatched right before a type is left. You can for example use this 63to add additional data for an object that you normally do not save inside 64objects such as links. 65 66**Event Object**: ``JMS\Serializer\EventDispatcher\ObjectEvent`` 67 68serializer.pre_deserialize 69~~~~~~~~~~~~~~~~~~~~~~~~~~~ 70 71.. versionadded : 0.12 72 Event was added 73 74This is dispatched before an object is deserialized. You can use this to 75modify submitted data, or modify the type that is being used for deserialization. 76 77**Event Object**: ``JMS\Serializer\EventDispatcher\PreDeserializeEvent`` 78 79serializer.post_deserialize 80~~~~~~~~~~~~~~~~~~~~~~~~~~~ 81This is dispatched after a type is processed. You can use it to normalize 82submitted data if you require external services for example, or also to 83perform validation of the submitted data. 84 85**Event Object**: ``JMS\Serializer\EventDispatcher\ObjectEvent`` 86