index.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ========
  2. Overview
  3. ========
  4. This bundle allows you to easily serialize/unserialize objects. Main features
  5. include:
  6. - able to handle circular references, and object graphs of any depth without
  7. writing a single line of code
  8. - serialize/unserialize objects using annotations to describe metadata
  9. - supports versioning out of the box
  10. - easily customizable as most logic is implemented using clearly defined
  11. interfaces
  12. You need to have control over the objects that you want to serialize/unserialize.
  13. This bundle does not work for objects provided by a third-party.
  14. TODO:
  15. - the unserialization process is not yet completely implemented (I currently
  16. don't need it, but contributions are welcome)
  17. Installation
  18. ------------
  19. Checkout a copy of the code::
  20. git submodule add https://github.com/schmittjoh/SerializerExtraBundle.git src/JMS/SerializerExtraBundle
  21. Then register the bundle with your kernel::
  22. // in AppKernel::registerBundles()
  23. $bundles = array(
  24. // ...
  25. new JMS\SerializerExtraBundle\SerializerExtraBundle(),
  26. // ...
  27. );
  28. Configuration
  29. -------------
  30. Below is the default configuration, you don't need to change it unless it doesn't
  31. suit your needs::
  32. jms_serializer_extra:
  33. naming_strategy:
  34. separator: _
  35. lower_case: true
  36. encoders:
  37. xml: true
  38. json: true
  39. Usage
  40. -----
  41. The bundle configures a factory, and a default serializer for you that you can
  42. use in your application code.
  43. The default serializer is used if you do not care about versioning::
  44. $serializer = $container->get('serializer');
  45. The serializer factory can be used if you want to display a specific version of
  46. an object::
  47. $factory = $container->get('serializer_factory');
  48. $serializer = $factory->getSerializer('1.0.0');
  49. Annotations
  50. -----------
  51. @ExclusionPolicy
  52. ~~~~~~~~~~~~~~~~
  53. This annotation can be defined on a class to indicate the exclusion strategy
  54. that should be used for the class.
  55. ::
  56. <?php
  57. /**
  58. * @ExclusionPolicy("NONE")
  59. */
  60. class MyObject
  61. {
  62. }
  63. +----------+----------------------------------------------------------------+
  64. | Policy | Description |
  65. +==========+================================================================+
  66. | all | all properties are excluded by default; only properties marked |
  67. | | with @Expose will be serialized/unserialized |
  68. +----------+----------------------------------------------------------------+
  69. | none | no properties are excluded by default; all properties except |
  70. | | those marked with @Exclude will be serialized/unserialized |
  71. +----------+----------------------------------------------------------------+
  72. @Exclude
  73. ~~~~~~~~
  74. This annotation can be defined on a property to indicate that the property should
  75. not be serialized/unserialized. Works only in combination with NoneExclusionPolicy.
  76. @Expose
  77. ~~~~~~~
  78. This annotation can be defined on a property to indicate that the property should
  79. be serialized/unserialized. Works only in combination with AllExclusionPolicy.
  80. @SerializedName
  81. ~~~~~~~~~~~~~~~
  82. This annotation can be defined on a property to define the serialized name for a
  83. property. If this is not defined, the property will be translated from camel-case
  84. to a lower-cased underscored name, e.g. camelCase -> camel_case.
  85. ::
  86. <?php
  87. class MyObject
  88. {
  89. /**
  90. * @SerializedName("some_other_name")
  91. */
  92. private $camelCase;
  93. }
  94. @Since
  95. ~~~~~~
  96. This annotation can be defined on a property to specify starting from which
  97. version this property is available. If an earlier version is serialized, then
  98. this property is excluded automatically.
  99. @Until
  100. ~~~~~~
  101. This annotation can be defined on a property to specify until which version this
  102. property was available. If a later version is serialized, then this property is
  103. excluded automatically.