annotations.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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\Serializer\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\Serializer\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\Serializer\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. @HandlerCallback
  141. ~~~~~~~~~~~~~~~~
  142. This annotation can be defined on a method if serialization/deserialization is handled
  143. by the object iself.
  144. .. code-block :: php
  145. <?php
  146. class Article
  147. {
  148. /**
  149. * @HandlerCallack("xml", direction = "serialization")
  150. */
  151. public function serializeToXml(XmlSerializationVisitor $visitor)
  152. {
  153. // custom logic here
  154. }
  155. }
  156. @Type
  157. ~~~~~
  158. This annotation can be defined on a property to specify the type of that property.
  159. For deserialization, this annotation must be defined. For serialization, you may
  160. define it in order to enhance the produced output; for example, you may want to
  161. force a certain format to be used for DateTime types.
  162. Available Types:
  163. +---------------------------+--------------------------------------------------+
  164. | Type | Description |
  165. +===========================+==================================================+
  166. | boolean | Primitive boolean |
  167. +---------------------------+--------------------------------------------------+
  168. | integer | Primitive integer |
  169. +---------------------------+--------------------------------------------------+
  170. | double | Primitive double |
  171. +---------------------------+--------------------------------------------------+
  172. | string | Primitive string |
  173. +---------------------------+--------------------------------------------------+
  174. | array | An array with arbitrary keys, and values. |
  175. +---------------------------+--------------------------------------------------+
  176. | array<T> | A list of type T (T can be any available type). |
  177. | | Examples: |
  178. | | array<string>, array<MyNamespace\MyObject>, etc. |
  179. +---------------------------+--------------------------------------------------+
  180. | array<K, V> | A map of keys of type K to values of type V. |
  181. | | Examples: array<string, string>, |
  182. | | array<string, MyNamespace\MyObject>, etc. |
  183. +---------------------------+--------------------------------------------------+
  184. | DateTime | PHP's DateTime object (default format/timezone) |
  185. +---------------------------+--------------------------------------------------+
  186. | DateTime<"format"> | PHP's DateTime object (custom format/default |
  187. | | timezone) |
  188. +---------------------------+--------------------------------------------------+
  189. | DateTime<"format", "zone">| PHP's DateTime object (custom format/timezone) |
  190. +---------------------------+--------------------------------------------------+
  191. | T | Where T is a fully qualified class name. |
  192. +---------------------------+--------------------------------------------------+
  193. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  194. | | into Doctrine's ArrayCollection class. |
  195. +---------------------------+--------------------------------------------------+
  196. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  197. | | into Doctrine's ArrayCollection class. |
  198. +---------------------------+--------------------------------------------------+
  199. Examples:
  200. .. code-block :: php
  201. <?php
  202. namespace MyNamespace;
  203. use JMS\Serializer\Annotation\Type;
  204. class BlogPost
  205. {
  206. /**
  207. * @Type("ArrayCollection<MyNamespace\Comment>")
  208. */
  209. private $comments;
  210. /**
  211. * @Type("string")
  212. */
  213. private $title;
  214. /**
  215. * @Type("MyNamespace\Author")
  216. */
  217. private $author;
  218. /**
  219. * @Type("DateTime")
  220. */
  221. private $createdAt;
  222. /**
  223. * @Type("boolean")
  224. */
  225. private $published;
  226. /**
  227. * @Type("array<string, string>")
  228. */
  229. private $keyValueStore;
  230. }
  231. @XmlRoot
  232. ~~~~~~~~
  233. This allows you to specify the name of the top-level element.
  234. .. code-block :: php
  235. <?php
  236. use JMS\Serializer\Annotation\XmlRoot;
  237. /** @XmlRoot("user") */
  238. class User
  239. {
  240. private $name = 'Johannes';
  241. }
  242. Resulting XML:
  243. .. code-block :: xml
  244. <user>
  245. <name><![CDATA[Johannes]]></name>
  246. </user>
  247. .. note ::
  248. @XmlRoot only applies to the root element, but is for example not taken into
  249. account for collections. You can define the entry name for collections using
  250. @XmlList, or @XmlMap.
  251. @XmlAttribute
  252. ~~~~~~~~~~~~~
  253. This allows you to mark properties which should be set as attributes,
  254. and not as child elements.
  255. .. code-block :: php
  256. <?php
  257. use JMS\Serializer\Annotation\XmlAttribute;
  258. class User
  259. {
  260. /** @XmlAttribute */
  261. private $id = 1;
  262. private $name = 'Johannes';
  263. }
  264. Resulting XML:
  265. .. code-block :: xml
  266. <result id="1">
  267. <name><![CDATA[Johannes]]></name>
  268. </result>
  269. @XmlValue
  270. ~~~~~~~~~
  271. This allows you to mark properties which should be set as the value of the
  272. current element. Note that this has the limitation that any additional
  273. properties of that object must have the @XmlAttribute annotation.
  274. .. code-block :: php
  275. <?php
  276. use JMS\Serializer\Annotation\XmlAttribute;
  277. use JMS\Serializer\Annotation\XmlValue;
  278. use JMS\Serializer\Annotation\XmlRoot;
  279. /** @XmlRoot("price") */
  280. class Price
  281. {
  282. /** @XmlAttribute */
  283. private $currency = 'EUR';
  284. /** @XmlValue */
  285. private $amount = 1.23;
  286. }
  287. Resulting XML:
  288. .. code-block :: xml
  289. <price currency="EUR">1.23</price>
  290. @XmlList
  291. ~~~~~~~~
  292. This allows you to define several properties of how arrays should be
  293. serialized. This is very similar to @XmlMap, and should be used if the
  294. keys of the array are not important.
  295. .. code-block :: php
  296. <?php
  297. use JMS\Serializer\Annotation\XmlList;
  298. use JMS\Serializer\Annotation\XmlRoot;
  299. /** @XmlRoot("post") */
  300. class Post
  301. {
  302. /**
  303. * @XmlList(inline = true, entry = "comment")
  304. */
  305. private $comments = array(
  306. new Comment('Foo'),
  307. new Comment('Bar'),
  308. );
  309. }
  310. class Comment
  311. {
  312. private $text;
  313. public function __construct($text)
  314. {
  315. $this->text = $text;
  316. }
  317. }
  318. Resulting XML:
  319. .. code-block :: xml
  320. <post>
  321. <comment>
  322. <text><![CDATA[Foo]]></text>
  323. </comment>
  324. <comment>
  325. <text><![CDATA[Bar]]></text>
  326. </comment>
  327. </post>
  328. @XmlMap
  329. ~~~~~~~
  330. Similar to @XmlList, but the keys of the array are meaningful.
  331. @XmlKeyValuePairs
  332. ~~~~~~~~~~~~~~~~~
  333. This allows you to use the keys of an array as xml tags.
  334. .. note ::
  335. When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key.
  336. @XmlAttributeMap
  337. ~~~~~~~~~~~~~~~~
  338. This is similar to the @XmlKeyValuePairs, but instead of creating child elements, it creates attributes.
  339. .. code-block :: php
  340. <?php
  341. use JMS\Serializer\Annotation\XmlAttribute;
  342. class Input
  343. {
  344. /** @XmlAttributeMap */
  345. private $id = array(
  346. 'name' => 'firstname',
  347. 'value' => 'Adrien',
  348. );
  349. }
  350. Resulting XML:
  351. .. code-block :: xml
  352. <result name="firstname" value="Adrien"/>