index.rst 9.0 KB

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