index.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. This bundle works best when you have full control over the objects that you want
  13. to serialize/unserialize as you can leverage the full power of annotations then.
  14. If you want to serialize/deserialize objects provided by third parties, then you
  15. need to write a custom normalizer for these objects.
  16. Installation
  17. ------------
  18. Checkout a copy of the code::
  19. git submodule add https://github.com/schmittjoh/SerializerBundle.git src/JMS/SerializerBundle
  20. Then register the bundle with your kernel::
  21. // in AppKernel::registerBundles()
  22. $bundles = array(
  23. // ...
  24. new JMS\SerializerBundle\JMSSerializerBundle(),
  25. // ...
  26. );
  27. Configuration
  28. -------------
  29. Below is the default configuration, you don't need to change it unless it doesn't
  30. suit your needs::
  31. jms_serializer:
  32. normalization:
  33. date_format: Y-m-d\TH:i:sO
  34. naming:
  35. separator: _
  36. lower_case: true
  37. doctrine_support: true
  38. # An array of version numbers: [1.0.0, 1.0.1, ...]
  39. versions: []
  40. Usage
  41. -----
  42. Factory vs. Default Instance
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. The bundle configures a factory, and a default serializer for you that you can
  45. use in your application code.
  46. The default serializer is used if you do not care about versioning::
  47. $serializer = $container->get('serializer');
  48. The serializer factory can be used if you want to display a specific version of
  49. an object::
  50. $factory = $container->get('serializer_factory');
  51. $serializer = $factory->getSerializer('1.0.0');
  52. Versioning
  53. ~~~~~~~~~~
  54. The bundle allows you to have different versions of your objects. This can be
  55. achieved by using the @Since, and @Until annotation which both accept a
  56. standardized PHP version number.
  57. ::
  58. <?php
  59. class VersionedObject
  60. {
  61. /**
  62. * @Until("1.0.x")
  63. */
  64. private $name;
  65. /**
  66. * @Since("1.1")
  67. * @SerializedName("name")
  68. */
  69. private $name2;
  70. }
  71. Changing the Exclusion Policy
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. The default exclusion policy is to exclude nothing, that is all properties of the
  74. object will be included in the normalized representation. If you only want to
  75. expose a few of the properties, then it is easier to change the exclusion policy,
  76. and only mark these few properties::
  77. <?php
  78. /**
  79. * @ExclusionPolicy("all")
  80. */
  81. class MyObject
  82. {
  83. private $foo;
  84. private $bar;
  85. /**
  86. * @Expose
  87. */
  88. private $name;
  89. }
  90. In the above example, only the "name" property will be included in the normalized
  91. representation.
  92. Annotations
  93. -----------
  94. @ExclusionPolicy
  95. ~~~~~~~~~~~~~~~~
  96. This annotation can be defined on a class to indicate the exclusion strategy
  97. that should be used for the class.
  98. +----------+----------------------------------------------------------------+
  99. | Policy | Description |
  100. +==========+================================================================+
  101. | all | all properties are excluded by default; only properties marked |
  102. | | with @Expose will be serialized/unserialized |
  103. +----------+----------------------------------------------------------------+
  104. | none | no properties are excluded by default; all properties except |
  105. | | those marked with @Exclude will be serialized/unserialized |
  106. +----------+----------------------------------------------------------------+
  107. @Exclude
  108. ~~~~~~~~
  109. This annotation can be defined on a property to indicate that the property should
  110. not be serialized/unserialized. Works only in combination with NoneExclusionPolicy.
  111. @Expose
  112. ~~~~~~~
  113. This annotation can be defined on a property to indicate that the property should
  114. be serialized/unserialized. Works only in combination with AllExclusionPolicy.
  115. @SerializedName
  116. ~~~~~~~~~~~~~~~
  117. This annotation can be defined on a property to define the serialized name for a
  118. property. If this is not defined, the property will be translated from camel-case
  119. to a lower-cased underscored name, e.g. camelCase -> camel_case.
  120. @Since
  121. ~~~~~~
  122. This annotation can be defined on a property to specify starting from which
  123. version this property is available. If an earlier version is serialized, then
  124. this property is excluded automatically. The version must be in a format that is
  125. understood by PHP's ``version_compare`` function.
  126. @Until
  127. ~~~~~~
  128. This annotation can be defined on a property to specify until which version this
  129. property was available. If a later version is serialized, then this property is
  130. excluded automatically. The version must be in a format that is understood by
  131. PHP's ``version_compare`` function.
  132. @Type
  133. ~~~~~
  134. This annotation can be defined on a property to specify the type of that property.
  135. This annotation must only be defined when you want to be able to deserialize an
  136. object.
  137. Available Types:
  138. +---------------------------+--------------------------------------------------+
  139. | Type | Description |
  140. +===========================+==================================================+
  141. | boolean | Primitive boolean |
  142. +---------------------------+--------------------------------------------------+
  143. | integer | Primitive integer |
  144. +---------------------------+--------------------------------------------------+
  145. | string | Primitive string |
  146. +---------------------------+--------------------------------------------------+
  147. | array | An array with arbitrary keys, and values. |
  148. +---------------------------+--------------------------------------------------+
  149. | array<T> | A list of type T (T can be any available type). |
  150. | | Examples: |
  151. | | array<string>, array<MyNamespace\MyObject>, etc. |
  152. +---------------------------+--------------------------------------------------+
  153. | array<K, V> | A map of keys of type K to values of type V. |
  154. | | Examples: array<string, string>, |
  155. | | array<string, MyNamespace\MyObject>, etc. |
  156. +---------------------------+--------------------------------------------------+
  157. | DateTime | PHP's DateTime object |
  158. +---------------------------+--------------------------------------------------+
  159. | T | Where T is a fully qualified class name. |
  160. +---------------------------+--------------------------------------------------+
  161. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  162. | | into Doctrine's ArrayCollection class. |
  163. +---------------------------+--------------------------------------------------+
  164. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  165. | | into Doctrine's ArrayCollection class. |
  166. +---------------------------+--------------------------------------------------+
  167. Examples::
  168. <?php
  169. namespace MyNamespace;
  170. use JMS\SerializerBundle\Annotation\Type;
  171. class BlogPost
  172. {
  173. /**
  174. * @Type("ArrayCollection<MyNamespace\Comment>")
  175. */
  176. private $comments;
  177. /**
  178. * @Type("string")
  179. */
  180. private $title;
  181. /**
  182. * @Type("MyNamespace\Author")
  183. */
  184. private $author;
  185. /**
  186. * @Type("DateTime")
  187. */
  188. private $createdAt;
  189. /**
  190. * @Type("boolean")
  191. */
  192. private $published;
  193. /**
  194. * @Type("array<string, string>")
  195. */
  196. private $keyValueStore;
  197. }