annotations.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /** @AccessType("public_method") */
  54. class User
  55. {
  56. private $name;
  57. public function getName()
  58. {
  59. return $this->name;
  60. }
  61. public function setName($name)
  62. {
  63. $this->name = trim($name);
  64. }
  65. }
  66. @Accessor
  67. ~~~~~~~~~
  68. This annotation can be defined on a property to specify which public method should
  69. be called to retrieve, or set the value of the given property:
  70. .. code-block :: php
  71. <?php
  72. class User
  73. {
  74. private $id;
  75. /** @Accessor(getter="getTrimmedName") */
  76. private $name;
  77. // ...
  78. public function getTrimmedName()
  79. {
  80. return trim($this->name);
  81. }
  82. public function setName($name)
  83. {
  84. $this->name = $name;
  85. }
  86. }
  87. @AccessorOrder
  88. ~~~~~~~~~~~~~~
  89. This annotation can be defined on a class to control the order of properties. By
  90. default the order is undefined, but you may change it to either "alphabetical", or
  91. "custom".
  92. .. code-block :: php
  93. <?php
  94. /**
  95. * @AccessorOrder("alphabetical")
  96. *
  97. * Resulting Property Order: id, name
  98. */
  99. class User
  100. {
  101. private $id;
  102. private $name;
  103. }
  104. /**
  105. * @AccessorOrder("custom", custom = {"name", "id"})
  106. *
  107. * Resulting Property Order: name, id
  108. */
  109. class User
  110. {
  111. private $id;
  112. private $name;
  113. }
  114. @Inline
  115. ~~~~~~~~
  116. This annotation can be defined on a property to indicate that the data of the property
  117. should be inlined.
  118. **Note**: This only works for serialization, the serializer will not be able to deserialize
  119. objects with this annotation. Also, AccessorOrder will be using the name of the property
  120. to determine the order.
  121. @ReadOnly
  122. ~~~~~~~~~
  123. This annotation can be defined on a property to indicate that the data of the property
  124. is read only and cannot be set during deserialization.
  125. @PreSerialize
  126. ~~~~~~~~~~~~~
  127. This annotation can be defined on a method which is supposed to be called before
  128. the serialization of the object starts.
  129. @PostSerialize
  130. ~~~~~~~~~~~~~~
  131. This annotation can be defined on a method which is then called directly after the
  132. object has been serialized.
  133. @PostDeserialize
  134. ~~~~~~~~~~~~~~~~
  135. This annotation can be defined on a method which is supposed to be called after
  136. the object has been deserialized.
  137. @Type
  138. ~~~~~
  139. This annotation can be defined on a property to specify the type of that property.
  140. This annotation must only be defined when you want to be able to deserialize an
  141. object.
  142. Available Types:
  143. +---------------------------+--------------------------------------------------+
  144. | Type | Description |
  145. +===========================+==================================================+
  146. | boolean | Primitive boolean |
  147. +---------------------------+--------------------------------------------------+
  148. | integer | Primitive integer |
  149. +---------------------------+--------------------------------------------------+
  150. | double | Primitive double |
  151. +---------------------------+--------------------------------------------------+
  152. | string | Primitive string |
  153. +---------------------------+--------------------------------------------------+
  154. | array | An array with arbitrary keys, and values. |
  155. +---------------------------+--------------------------------------------------+
  156. | array<T> | A list of type T (T can be any available type). |
  157. | | Examples: |
  158. | | array<string>, array<MyNamespace\MyObject>, etc. |
  159. +---------------------------+--------------------------------------------------+
  160. | array<K, V> | A map of keys of type K to values of type V. |
  161. | | Examples: array<string, string>, |
  162. | | array<string, MyNamespace\MyObject>, etc. |
  163. +---------------------------+--------------------------------------------------+
  164. | DateTime | PHP's DateTime object |
  165. +---------------------------+--------------------------------------------------+
  166. | T | Where T is a fully qualified class name. |
  167. +---------------------------+--------------------------------------------------+
  168. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  169. | | into Doctrine's ArrayCollection class. |
  170. +---------------------------+--------------------------------------------------+
  171. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  172. | | into Doctrine's ArrayCollection class. |
  173. +---------------------------+--------------------------------------------------+
  174. Examples:
  175. .. code-block :: php
  176. <?php
  177. namespace MyNamespace;
  178. use JMS\SerializerBundle\Annotation\Type;
  179. class BlogPost
  180. {
  181. /**
  182. * @Type("ArrayCollection<MyNamespace\Comment>")
  183. */
  184. private $comments;
  185. /**
  186. * @Type("string")
  187. */
  188. private $title;
  189. /**
  190. * @Type("MyNamespace\Author")
  191. */
  192. private $author;
  193. /**
  194. * @Type("DateTime")
  195. */
  196. private $createdAt;
  197. /**
  198. * @Type("boolean")
  199. */
  200. private $published;
  201. /**
  202. * @Type("array<string, string>")
  203. */
  204. private $keyValueStore;
  205. }
  206. @XmlRoot
  207. ~~~~~~~~
  208. This allows you to specify the name of the top-level element.
  209. .. code-block :: php
  210. <?php
  211. use JMS\SerializerBundle\Annotation\XmlRoot;
  212. /** @XmlRoot("user") */
  213. class User
  214. {
  215. private $name = 'Johannes';
  216. }
  217. Resulting XML:
  218. .. code-block :: xml
  219. <user>
  220. <name><![CDATA[Johannes]]></name>
  221. </user>
  222. @XmlAttribute
  223. ~~~~~~~~~~~~~
  224. This allows you to mark properties which should be set as attributes,
  225. and not as child elements.
  226. .. code-block :: php
  227. <?php
  228. use JMS\SerializerBundle\Annotation\XmlAttribute;
  229. class User
  230. {
  231. /** @XmlAttribute */
  232. private $id = 1;
  233. private $name = 'Johannes';
  234. }
  235. Resulting XML:
  236. .. code-block :: xml
  237. <result id="1">
  238. <name><![CDATA[Johannes]]></name>
  239. </result>
  240. @XmlValue
  241. ~~~~~~~~~
  242. This allows you to mark properties which should be set as the value of the
  243. current element. Note that this has the limitation that any additional
  244. properties of that object must have the @XmlAttribute annotation.
  245. .. code-block :: php
  246. <?php
  247. use JMS\SerializerBundle\Annotation\XmlAttribute;
  248. use JMS\SerializerBundle\Annotation\XmlValue;
  249. use JMS\SerializerBundle\Annotation\XmlRoot;
  250. /** @XmlRoot("price") */
  251. class Price
  252. {
  253. /** @XmlAttribute */
  254. private $currency = 'EUR';
  255. /** @XmlValue */
  256. private $amount = 1.23;
  257. }
  258. Resulting XML:
  259. .. code-block :: xml
  260. <price currency="EUR">1.23</price>
  261. @XmlList
  262. ~~~~~~~~
  263. This allows you to define several properties of how arrays should be
  264. serialized. This is very similar to @XmlMap, and should be used if the
  265. keys of the array are not important.
  266. .. code-block :: php
  267. <?php
  268. use JMS\SerializerBundle\Annotation\XmlList;
  269. use JMS\SerializerBundle\Annotation\XmlRoot;
  270. /** @XmlRoot("post") */
  271. class Post
  272. {
  273. /**
  274. * @XmlList(inline = true, entry = "comment")
  275. */
  276. private $comments = array(
  277. new Comment('Foo'),
  278. new Comment('Bar'),
  279. );
  280. }
  281. class Comment
  282. {
  283. private $text;
  284. public function __construct($text)
  285. {
  286. $this->text = $text;
  287. }
  288. }
  289. Resulting XML:
  290. .. code-block :: xml
  291. <post>
  292. <comment>
  293. <text><![CDATA[Foo]]></text>
  294. </comment>
  295. <comment>
  296. <text><![CDATA[Bar]]></text>
  297. </comment>
  298. </post>
  299. @XmlMap
  300. ~~~~~~~
  301. Similar to @XmlList, but the keys of the array are meaningful.