annotations.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. Annotations
  2. -----------
  3. @ExclusionPolicy
  4. ~~~~~~~~~~~~~~~~
  5. This annotation can be defined on a class to indicate the exclusion strategy
  6. that should be used for the class.
  7. +----------+----------------------------------------------------------------+
  8. | Policy | Description |
  9. +==========+================================================================+
  10. | all | all properties are excluded by default; only properties marked |
  11. | | with @Expose will be serialized/unserialized |
  12. +----------+----------------------------------------------------------------+
  13. | none | no properties are excluded by default; all properties except |
  14. | | those marked with @Exclude will be serialized/unserialized |
  15. +----------+----------------------------------------------------------------+
  16. @Exclude
  17. ~~~~~~~~
  18. This annotation can be defined on a property to indicate that the property should
  19. not be serialized/unserialized. Works only in combination with NoneExclusionPolicy.
  20. @Expose
  21. ~~~~~~~
  22. This annotation can be defined on a property to indicate that the property should
  23. be serialized/unserialized. Works only in combination with AllExclusionPolicy.
  24. @SerializedName
  25. ~~~~~~~~~~~~~~~
  26. This annotation can be defined on a property to define the serialized name for a
  27. property. If this is not defined, the property will be translated from camel-case
  28. to a lower-cased underscored name, e.g. camelCase -> camel_case.
  29. @Since
  30. ~~~~~~
  31. This annotation can be defined on a property to specify starting from which
  32. version this property is available. If an earlier version is serialized, then
  33. this property is excluded automatically. The version must be in a format that is
  34. understood by PHP's ``version_compare`` function.
  35. @Until
  36. ~~~~~~
  37. This annotation can be defined on a property to specify until which version this
  38. property was available. If a later version is serialized, then this property is
  39. excluded automatically. The version must be in a format that is understood by
  40. PHP's ``version_compare`` function.
  41. @Groups
  42. ~~~~~~~
  43. This annotation can be defined on a property to specifiy to if the property
  44. should be serialized when only serializing specific groups (see
  45. :doc:`../cookbook/exclusion_strategies`).
  46. @AccessType
  47. ~~~~~~~~~~~
  48. This annotation can be defined on a property, or a class to specify in which way
  49. the properties should be accessed. By default, the serializer will retrieve, or
  50. set the value via reflection, but you may change this to use a public method instead:
  51. .. code-block :: php
  52. <?php
  53. use JMS\SerializerBundle\Annotation\AccessType;
  54. /** @AccessType("public_method") */
  55. class User
  56. {
  57. private $name;
  58. public function getName()
  59. {
  60. return $this->name;
  61. }
  62. public function setName($name)
  63. {
  64. $this->name = trim($name);
  65. }
  66. }
  67. @Accessor
  68. ~~~~~~~~~
  69. This annotation can be defined on a property to specify which public method should
  70. be called to retrieve, or set the value of the given property:
  71. .. code-block :: php
  72. <?php
  73. use JMS\SerializerBundle\Annotation\Accessor;
  74. class User
  75. {
  76. private $id;
  77. /** @Accessor(getter="getTrimmedName") */
  78. private $name;
  79. // ...
  80. public function getTrimmedName()
  81. {
  82. return trim($this->name);
  83. }
  84. public function setName($name)
  85. {
  86. $this->name = $name;
  87. }
  88. }
  89. @AccessorOrder
  90. ~~~~~~~~~~~~~~
  91. This annotation can be defined on a class to control the order of properties. By
  92. default the order is undefined, but you may change it to either "alphabetical", or
  93. "custom".
  94. .. code-block :: php
  95. <?php
  96. use JMS\SerializerBundle\Annotation\AccessorOrder;
  97. /**
  98. * @AccessorOrder("alphabetical")
  99. *
  100. * Resulting Property Order: id, name
  101. */
  102. class User
  103. {
  104. private $id;
  105. private $name;
  106. }
  107. /**
  108. * @AccessorOrder("custom", custom = {"name", "id"})
  109. *
  110. * Resulting Property Order: name, id
  111. */
  112. class User
  113. {
  114. private $id;
  115. private $name;
  116. }
  117. @Inline
  118. ~~~~~~~~
  119. This annotation can be defined on a property to indicate that the data of the property
  120. should be inlined.
  121. **Note**: This only works for serialization, the serializer will not be able to deserialize
  122. objects with this annotation. Also, AccessorOrder will be using the name of the property
  123. to determine the order.
  124. @ReadOnly
  125. ~~~~~~~~~
  126. This annotation can be defined on a property to indicate that the data of the property
  127. is read only and cannot be set during deserialization.
  128. @PreSerialize
  129. ~~~~~~~~~~~~~
  130. This annotation can be defined on a method which is supposed to be called before
  131. the serialization of the object starts.
  132. @PostSerialize
  133. ~~~~~~~~~~~~~~
  134. This annotation can be defined on a method which is then called directly after the
  135. object has been serialized.
  136. @PostDeserialize
  137. ~~~~~~~~~~~~~~~~
  138. This annotation can be defined on a method which is supposed to be called after
  139. the object has been deserialized.
  140. @Type
  141. ~~~~~
  142. This annotation can be defined on a property to specify the type of that property.
  143. This annotation must only be defined when you want to be able to deserialize an
  144. object.
  145. Available Types:
  146. +---------------------------+--------------------------------------------------+
  147. | Type | Description |
  148. +===========================+==================================================+
  149. | boolean | Primitive boolean |
  150. +---------------------------+--------------------------------------------------+
  151. | integer | Primitive integer |
  152. +---------------------------+--------------------------------------------------+
  153. | double | Primitive double |
  154. +---------------------------+--------------------------------------------------+
  155. | string | Primitive string |
  156. +---------------------------+--------------------------------------------------+
  157. | array | An array with arbitrary keys, and values. |
  158. +---------------------------+--------------------------------------------------+
  159. | array<T> | A list of type T (T can be any available type). |
  160. | | Examples: |
  161. | | array<string>, array<MyNamespace\MyObject>, etc. |
  162. +---------------------------+--------------------------------------------------+
  163. | array<K, V> | A map of keys of type K to values of type V. |
  164. | | Examples: array<string, string>, |
  165. | | array<string, MyNamespace\MyObject>, etc. |
  166. +---------------------------+--------------------------------------------------+
  167. | DateTime | PHP's DateTime object |
  168. +---------------------------+--------------------------------------------------+
  169. | T | Where T is a fully qualified class name. |
  170. +---------------------------+--------------------------------------------------+
  171. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  172. | | into Doctrine's ArrayCollection class. |
  173. +---------------------------+--------------------------------------------------+
  174. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  175. | | into Doctrine's ArrayCollection class. |
  176. +---------------------------+--------------------------------------------------+
  177. Examples:
  178. .. code-block :: php
  179. <?php
  180. namespace MyNamespace;
  181. use JMS\SerializerBundle\Annotation\Type;
  182. class BlogPost
  183. {
  184. /**
  185. * @Type("ArrayCollection<MyNamespace\Comment>")
  186. */
  187. private $comments;
  188. /**
  189. * @Type("string")
  190. */
  191. private $title;
  192. /**
  193. * @Type("MyNamespace\Author")
  194. */
  195. private $author;
  196. /**
  197. * @Type("DateTime")
  198. */
  199. private $createdAt;
  200. /**
  201. * @Type("boolean")
  202. */
  203. private $published;
  204. /**
  205. * @Type("array<string, string>")
  206. */
  207. private $keyValueStore;
  208. }
  209. @XmlRoot
  210. ~~~~~~~~
  211. This allows you to specify the name of the top-level element.
  212. .. code-block :: php
  213. <?php
  214. use JMS\SerializerBundle\Annotation\XmlRoot;
  215. /** @XmlRoot("user") */
  216. class User
  217. {
  218. private $name = 'Johannes';
  219. }
  220. Resulting XML:
  221. .. code-block :: xml
  222. <user>
  223. <name><![CDATA[Johannes]]></name>
  224. </user>
  225. @XmlAttribute
  226. ~~~~~~~~~~~~~
  227. This allows you to mark properties which should be set as attributes,
  228. and not as child elements.
  229. .. code-block :: php
  230. <?php
  231. use JMS\SerializerBundle\Annotation\XmlAttribute;
  232. class User
  233. {
  234. /** @XmlAttribute */
  235. private $id = 1;
  236. private $name = 'Johannes';
  237. }
  238. Resulting XML:
  239. .. code-block :: xml
  240. <result id="1">
  241. <name><![CDATA[Johannes]]></name>
  242. </result>
  243. @XmlValue
  244. ~~~~~~~~~
  245. This allows you to mark properties which should be set as the value of the
  246. current element. Note that this has the limitation that any additional
  247. properties of that object must have the @XmlAttribute annotation.
  248. .. code-block :: php
  249. <?php
  250. use JMS\SerializerBundle\Annotation\XmlAttribute;
  251. use JMS\SerializerBundle\Annotation\XmlValue;
  252. use JMS\SerializerBundle\Annotation\XmlRoot;
  253. /** @XmlRoot("price") */
  254. class Price
  255. {
  256. /** @XmlAttribute */
  257. private $currency = 'EUR';
  258. /** @XmlValue */
  259. private $amount = 1.23;
  260. }
  261. Resulting XML:
  262. .. code-block :: xml
  263. <price currency="EUR">1.23</price>
  264. @XmlList
  265. ~~~~~~~~
  266. This allows you to define several properties of how arrays should be
  267. serialized. This is very similar to @XmlMap, and should be used if the
  268. keys of the array are not important.
  269. .. code-block :: php
  270. <?php
  271. use JMS\SerializerBundle\Annotation\XmlList;
  272. use JMS\SerializerBundle\Annotation\XmlRoot;
  273. /** @XmlRoot("post") */
  274. class Post
  275. {
  276. /**
  277. * @XmlList(inline = true, entry = "comment")
  278. */
  279. private $comments = array(
  280. new Comment('Foo'),
  281. new Comment('Bar'),
  282. );
  283. }
  284. class Comment
  285. {
  286. private $text;
  287. public function __construct($text)
  288. {
  289. $this->text = $text;
  290. }
  291. }
  292. Resulting XML:
  293. .. code-block :: xml
  294. <post>
  295. <comment>
  296. <text><![CDATA[Foo]]></text>
  297. </comment>
  298. <comment>
  299. <text><![CDATA[Bar]]></text>
  300. </comment>
  301. </post>
  302. @XmlMap
  303. ~~~~~~~
  304. Similar to @XmlList, but the keys of the array are meaningful.
  305. @XmlKeyValuePairs
  306. ~~~~~~~~~~~~~~~~~
  307. This allows you to use the keys of an array as xml tags.
  308. .. note ::
  309. When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key.