index.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/SerializerBundle.git src/JMS/SerializerBundle
  21. Then register the bundle with your kernel::
  22. // in AppKernel::registerBundles()
  23. $bundles = array(
  24. // ...
  25. new JMS\SerializerBundle\JMSSerializerBundle(),
  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:
  33. naming_strategy:
  34. separator: _
  35. lower_case: true
  36. encoders:
  37. xml: true
  38. json: true
  39. Usage
  40. -----
  41. Factory vs. Default Instance
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. The bundle configures a factory, and a default serializer for you that you can
  44. use in your application code.
  45. The default serializer is used if you do not care about versioning::
  46. $serializer = $container->get('serializer');
  47. The serializer factory can be used if you want to display a specific version of
  48. an object::
  49. $factory = $container->get('serializer_factory');
  50. $serializer = $factory->getSerializer('1.0.0');
  51. Versioning
  52. ~~~~~~~~~~
  53. The bundle allows you to have different versions of your objects. This can be
  54. achieved by using the @Since, and @Until annotation which both accept a
  55. standardized PHP version number.
  56. ::
  57. <?php
  58. class VersionedObject
  59. {
  60. /**
  61. * @Until("1.0.x")
  62. */
  63. private $name;
  64. /**
  65. * @Since("1.1")
  66. * @SerializedName("name")
  67. */
  68. private $name2;
  69. }
  70. Changing the Exclusion Policy
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. The default exclusion policy is to exclude nothing, that is all properties of the
  73. object will be included in the normalized representation. If you only want to
  74. expose a few of the properties, then it is easier to change the exclusion policy,
  75. and only mark these few properties::
  76. <?php
  77. /**
  78. * @ExclusionPolicy("all")
  79. */
  80. class MyObject
  81. {
  82. private $foo;
  83. private $bar;
  84. /**
  85. * @Expose
  86. */
  87. private $name;
  88. }
  89. In the above example, only the "name" property will be included in the normalized
  90. representation.
  91. Annotations
  92. -----------
  93. @ExclusionPolicy
  94. ~~~~~~~~~~~~~~~~
  95. This annotation can be defined on a class to indicate the exclusion strategy
  96. that should be used for the class.
  97. +----------+----------------------------------------------------------------+
  98. | Policy | Description |
  99. +==========+================================================================+
  100. | all | all properties are excluded by default; only properties marked |
  101. | | with @Expose will be serialized/unserialized |
  102. +----------+----------------------------------------------------------------+
  103. | none | no properties are excluded by default; all properties except |
  104. | | those marked with @Exclude will be serialized/unserialized |
  105. +----------+----------------------------------------------------------------+
  106. @Exclude
  107. ~~~~~~~~
  108. This annotation can be defined on a property to indicate that the property should
  109. not be serialized/unserialized. Works only in combination with NoneExclusionPolicy.
  110. @Expose
  111. ~~~~~~~
  112. This annotation can be defined on a property to indicate that the property should
  113. be serialized/unserialized. Works only in combination with AllExclusionPolicy.
  114. @SerializedName
  115. ~~~~~~~~~~~~~~~
  116. This annotation can be defined on a property to define the serialized name for a
  117. property. If this is not defined, the property will be translated from camel-case
  118. to a lower-cased underscored name, e.g. camelCase -> camel_case.
  119. @Since
  120. ~~~~~~
  121. This annotation can be defined on a property to specify starting from which
  122. version this property is available. If an earlier version is serialized, then
  123. this property is excluded automatically. The version must be in a format that is
  124. understood by PHP's ``version_compare`` function.
  125. @Until
  126. ~~~~~~
  127. This annotation can be defined on a property to specify until which version this
  128. property was available. If a later version is serialized, then this property is
  129. excluded automatically. The version must be in a format that is understood by
  130. PHP's ``version_compare`` function.