annotations.rst 11 KB

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