event_system.rst 2.9 KB

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