annotations.rst 11 KB

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