configuration.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Configuration
  2. =============
  3. .. note ::
  4. If you are using Symfony2, this section is mostly irrelevant for you as the entire integration is provided by
  5. JMSSerializerBundle; please see `its documentation <http://jmsyst.com/bundles/JMSSerializerBundle>`_. If you are
  6. using another framework, there also might be a module, or other special integration. Please check packagist, or
  7. whatever registry usually holds such information for your framework.
  8. Constructing a Serializer
  9. -------------------------
  10. This library provides a special builder object which makes constructing serializer instances a breeze in any PHP
  11. project. In its shortest version, it's just a single line of code::
  12. $serializer = JMS\Serializer\SerializerBuilder::create()->build();
  13. This serializer is fully functional, but you might want to tweak it a bit for example to configure a cache directory.
  14. Configuring a Cache Directory
  15. -----------------------------
  16. The serializer collects several metadata about your objects from various sources such as YML, XML, or annotations. In
  17. order to make this process as efficient as possible, it is encourage to let the serializer cache that information. For
  18. that, you can configure a cache directory::
  19. $builder = new JMS\Serializer\SerializerBuilder();
  20. $serializer =
  21. JMS\Serializer\SerializerBuilder::create()
  22. ->setCacheDir($someWritableDir)
  23. ->setDebug($trueOrFalse)
  24. ->build();
  25. As you can see, we also added a call to the ``setDebug`` method. In debug mode, the serializer will perform a bit more
  26. filesystem checks to see whether the data that it has cached is still valid. These checks are useful during development
  27. so that you do not need to manually clear cache folders, however in production they are just unnecessary overhead. The
  28. debug setting allows you to make the behavior environment specific.
  29. Adding Custom Handlers
  30. ----------------------
  31. If you have created custom handlers, you can add them to the serializer easily::
  32. $serializer =
  33. JMS\Serializer\SerializerBuilder::create()
  34. ->addDefaultHandlers()
  35. ->configureHandlers(function(JMS\Serializer\Handler\HandlerRegistry $registry) {
  36. $registry->registerHandler('serialization', 'MyObject', 'json',
  37. function($visitor, MyObject $obj, array $type) {
  38. return $obj->getName();
  39. }
  40. );
  41. })
  42. ->build();
  43. For more complex handlers, it is advisable to extract them to dedicated classes,
  44. see :doc:`handlers documentation <handlers>`.
  45. Configuring Metadata Locations
  46. ------------------------------
  47. This library supports several metadata sources. By default, it uses Doctrine annotations, but you may also store
  48. metadata in XML, or YML files. For the latter, it is necessary to configure a metadata directory where those files
  49. are located::
  50. $serializer =
  51. JMS\Serializer\SerializerBuilder::create()
  52. ->addMetadataDir($someDir)
  53. ->build();
  54. The serializer would expect the metadata files to be named like the fully qualified class names where all ``\`` are
  55. replaced with ``.``. So, if you class would be named ``Vendor\Package\Foo``, the metadata file would need to be located
  56. at ``$someDir/Vendor.Package.Foo.(xml|yml)``. For more information, see the :doc:`reference <reference>`.
  57. Setting a default SerializationContext factory
  58. --------------------------------------------
  59. To avoid to pass an instance of SerializationContext
  60. every time you call method ``serialize()`` (or ``toArray()``),
  61. you can set a ``SerializationContextFactory`` to the Serializer.
  62. Example using the SerializerBuilder::
  63. use JMS\Serializer\SerializationContext;
  64. $serializer = JMS\Serializer\SerializerBuilder::create()
  65. ->setSerializationContextFactory(function () {
  66. return SerializationContext::create()
  67. ->setSerializeNull(true)
  68. ;
  69. })
  70. ->build()
  71. ;
  72. Then, calling ``$serializer->serialize($data, 'json');`` will generate
  73. a serialization context from your callable and use it.
  74. .. note ::
  75. You can also set a default DeserializationContextFactory with
  76. ``->setDeserializationContextFactory(function () { /* ... */ })``
  77. to be used with methods ``deserialize()`` and ``fromArray()``.