annotations.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. @MaxDepth
  47. ~~~~~~~~~
  48. This annotation can be defined on a property to limit the depth to which the
  49. content will be serialized. It is very useful when a property will contain a
  50. large object graph.
  51. @AccessType
  52. ~~~~~~~~~~~
  53. This annotation can be defined on a property, or a class to specify in which way
  54. the properties should be accessed. By default, the serializer will retrieve, or
  55. set the value via reflection, but you may change this to use a public method instead:
  56. .. code-block :: php
  57. <?php
  58. use JMS\Serializer\Annotation\AccessType;
  59. /** @AccessType("public_method") */
  60. class User
  61. {
  62. private $name;
  63. public function getName()
  64. {
  65. return $this->name;
  66. }
  67. public function setName($name)
  68. {
  69. $this->name = trim($name);
  70. }
  71. }
  72. @Accessor
  73. ~~~~~~~~~
  74. This annotation can be defined on a property to specify which public method should
  75. be called to retrieve, or set the value of the given property:
  76. .. code-block :: php
  77. <?php
  78. use JMS\Serializer\Annotation\Accessor;
  79. class User
  80. {
  81. private $id;
  82. /** @Accessor(getter="getTrimmedName",setter="setName") */
  83. private $name;
  84. // ...
  85. public function getTrimmedName()
  86. {
  87. return trim($this->name);
  88. }
  89. public function setName($name)
  90. {
  91. $this->name = $name;
  92. }
  93. }
  94. @AccessorOrder
  95. ~~~~~~~~~~~~~~
  96. This annotation can be defined on a class to control the order of properties. By
  97. default the order is undefined, but you may change it to either "alphabetical", or
  98. "custom".
  99. .. code-block :: php
  100. <?php
  101. use JMS\Serializer\Annotation\AccessorOrder;
  102. /**
  103. * @AccessorOrder("alphabetical")
  104. *
  105. * Resulting Property Order: id, name
  106. */
  107. class User
  108. {
  109. private $id;
  110. private $name;
  111. }
  112. /**
  113. * @AccessorOrder("custom", custom = {"name", "id"})
  114. *
  115. * Resulting Property Order: name, id
  116. */
  117. class User
  118. {
  119. private $id;
  120. private $name;
  121. }
  122. /**
  123. * @AccessorOrder("custom", custom = {"name", "SomeMethod" ,"id"})
  124. *
  125. * Resulting Property Order: name, mood, id
  126. */
  127. class User
  128. {
  129. private $id;
  130. private $name;
  131. /**
  132. * @Serializer\VirtualProperty
  133. * @Serializer\SerializedName("mood")
  134. *
  135. * @return string
  136. */
  137. public function getSomeMethod()
  138. {
  139. return 'happy';
  140. }
  141. }
  142. @VirtualProperty
  143. ~~~~~~~~~~~~~~~~
  144. This annotation can be defined on a method to indicate that the data returned by
  145. the method should appear like a property of the object.
  146. **Note**: This only works for serialization and is completely ignored during
  147. deserialization.
  148. @Inline
  149. ~~~~~~~~
  150. This annotation can be defined on a property to indicate that the data of the property
  151. should be inlined.
  152. **Note**: This only works for serialization, the serializer will not be able to deserialize
  153. objects with this annotation. Also, AccessorOrder will be using the name of the property
  154. to determine the order.
  155. @ReadOnly
  156. ~~~~~~~~~
  157. This annotation can be defined on a property to indicate that the data of the property
  158. is read only and cannot be set during deserialization.
  159. @PreSerialize
  160. ~~~~~~~~~~~~~
  161. This annotation can be defined on a method which is supposed to be called before
  162. the serialization of the object starts.
  163. @PostSerialize
  164. ~~~~~~~~~~~~~~
  165. This annotation can be defined on a method which is then called directly after the
  166. object has been serialized.
  167. @PostDeserialize
  168. ~~~~~~~~~~~~~~~~
  169. This annotation can be defined on a method which is supposed to be called after
  170. the object has been deserialized.
  171. @HandlerCallback
  172. ~~~~~~~~~~~~~~~~
  173. This annotation can be defined on a method if serialization/deserialization is handled
  174. by the object iself.
  175. .. code-block :: php
  176. <?php
  177. class Article
  178. {
  179. /**
  180. * @HandlerCallback("xml", direction = "serialization")
  181. */
  182. public function serializeToXml(XmlSerializationVisitor $visitor)
  183. {
  184. // custom logic here
  185. }
  186. }
  187. @Discriminator
  188. ~~~~~~~~~~~~~~
  189. .. versionadded : 0.12
  190. @Discriminator was added
  191. This annotation allows deserialization of relations which are polymorphic, but
  192. where a common base class exists. The ``@Discriminator`` annotation has to be applied
  193. to the least super type::
  194. /**
  195. * @Discriminator(field = "type", map = {"car": "Car", "moped": "Moped"})
  196. */
  197. abstract class Vehicle { }
  198. class Car extends Vehicle { }
  199. class Moped extends Vehicle { }
  200. @Type
  201. ~~~~~
  202. This annotation can be defined on a property to specify the type of that property.
  203. For deserialization, this annotation must be defined. For serialization, you may
  204. define it in order to enhance the produced output; for example, you may want to
  205. force a certain format to be used for DateTime types.
  206. Available Types:
  207. +---------------------------+--------------------------------------------------+
  208. | Type | Description |
  209. +===========================+==================================================+
  210. | boolean | Primitive boolean |
  211. +---------------------------+--------------------------------------------------+
  212. | integer | Primitive integer |
  213. +---------------------------+--------------------------------------------------+
  214. | double | Primitive double |
  215. +---------------------------+--------------------------------------------------+
  216. | string | Primitive string |
  217. +---------------------------+--------------------------------------------------+
  218. | array | An array with arbitrary keys, and values. |
  219. +---------------------------+--------------------------------------------------+
  220. | array<T> | A list of type T (T can be any available type). |
  221. | | Examples: |
  222. | | array<string>, array<MyNamespace\MyObject>, etc. |
  223. +---------------------------+--------------------------------------------------+
  224. | array<K, V> | A map of keys of type K to values of type V. |
  225. | | Examples: array<string, string>, |
  226. | | array<string, MyNamespace\MyObject>, etc. |
  227. +---------------------------+--------------------------------------------------+
  228. | DateTime | PHP's DateTime object (default format/timezone) |
  229. +---------------------------+--------------------------------------------------+
  230. | DateTime<'format'> | PHP's DateTime object (custom format/default |
  231. | | timezone) |
  232. +---------------------------+--------------------------------------------------+
  233. | DateTime<'format', 'zone'>| PHP's DateTime object (custom format/timezone) |
  234. +---------------------------+--------------------------------------------------+
  235. | T | Where T is a fully qualified class name. |
  236. +---------------------------+--------------------------------------------------+
  237. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  238. | | into Doctrine's ArrayCollection class. |
  239. +---------------------------+--------------------------------------------------+
  240. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  241. | | into Doctrine's ArrayCollection class. |
  242. +---------------------------+--------------------------------------------------+
  243. Examples:
  244. .. code-block :: php
  245. <?php
  246. namespace MyNamespace;
  247. use JMS\Serializer\Annotation\Type;
  248. class BlogPost
  249. {
  250. /**
  251. * @Type("ArrayCollection<MyNamespace\Comment>")
  252. */
  253. private $comments;
  254. /**
  255. * @Type("string")
  256. */
  257. private $title;
  258. /**
  259. * @Type("MyNamespace\Author")
  260. */
  261. private $author;
  262. /**
  263. * @Type("DateTime")
  264. */
  265. private $createdAt;
  266. /**
  267. * @Type("DateTime<'Y-m-d'>")
  268. */
  269. private $updatedAt;
  270. /**
  271. * @Type("boolean")
  272. */
  273. private $published;
  274. /**
  275. * @Type("array<string, string>")
  276. */
  277. private $keyValueStore;
  278. }
  279. @XmlRoot
  280. ~~~~~~~~
  281. This allows you to specify the name of the top-level element.
  282. .. code-block :: php
  283. <?php
  284. use JMS\Serializer\Annotation\XmlRoot;
  285. /** @XmlRoot("user") */
  286. class User
  287. {
  288. private $name = 'Johannes';
  289. }
  290. Resulting XML:
  291. .. code-block :: xml
  292. <user>
  293. <name><![CDATA[Johannes]]></name>
  294. </user>
  295. .. note ::
  296. @XmlRoot only applies to the root element, but is for example not taken into
  297. account for collections. You can define the entry name for collections using
  298. @XmlList, or @XmlMap.
  299. @XmlAttribute
  300. ~~~~~~~~~~~~~
  301. This allows you to mark properties which should be set as attributes,
  302. and not as child elements.
  303. .. code-block :: php
  304. <?php
  305. use JMS\Serializer\Annotation\XmlAttribute;
  306. class User
  307. {
  308. /** @XmlAttribute */
  309. private $id = 1;
  310. private $name = 'Johannes';
  311. }
  312. Resulting XML:
  313. .. code-block :: xml
  314. <result id="1">
  315. <name><![CDATA[Johannes]]></name>
  316. </result>
  317. @XmlValue
  318. ~~~~~~~~~
  319. This allows you to mark properties which should be set as the value of the
  320. current element. Note that this has the limitation that any additional
  321. properties of that object must have the @XmlAttribute annotation.
  322. .. code-block :: php
  323. <?php
  324. use JMS\Serializer\Annotation\XmlAttribute;
  325. use JMS\Serializer\Annotation\XmlValue;
  326. use JMS\Serializer\Annotation\XmlRoot;
  327. /** @XmlRoot("price") */
  328. class Price
  329. {
  330. /** @XmlAttribute */
  331. private $currency = 'EUR';
  332. /** @XmlValue */
  333. private $amount = 1.23;
  334. }
  335. Resulting XML:
  336. .. code-block :: xml
  337. <price currency="EUR">1.23</price>
  338. @XmlList
  339. ~~~~~~~~
  340. This allows you to define several properties of how arrays should be
  341. serialized. This is very similar to @XmlMap, and should be used if the
  342. keys of the array are not important.
  343. .. code-block :: php
  344. <?php
  345. use JMS\Serializer\Annotation\XmlList;
  346. use JMS\Serializer\Annotation\XmlRoot;
  347. /** @XmlRoot("post") */
  348. class Post
  349. {
  350. /**
  351. * @XmlList(inline = true, entry = "comment")
  352. */
  353. private $comments = array(
  354. new Comment('Foo'),
  355. new Comment('Bar'),
  356. );
  357. }
  358. class Comment
  359. {
  360. private $text;
  361. public function __construct($text)
  362. {
  363. $this->text = $text;
  364. }
  365. }
  366. Resulting XML:
  367. .. code-block :: xml
  368. <post>
  369. <comment>
  370. <text><![CDATA[Foo]]></text>
  371. </comment>
  372. <comment>
  373. <text><![CDATA[Bar]]></text>
  374. </comment>
  375. </post>
  376. @XmlMap
  377. ~~~~~~~
  378. Similar to @XmlList, but the keys of the array are meaningful.
  379. @XmlKeyValuePairs
  380. ~~~~~~~~~~~~~~~~~
  381. This allows you to use the keys of an array as xml tags.
  382. .. note ::
  383. When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key.
  384. @XmlAttributeMap
  385. ~~~~~~~~~~~~~~~~
  386. This is similar to the @XmlKeyValuePairs, but instead of creating child elements, it creates attributes.
  387. .. code-block :: php
  388. <?php
  389. use JMS\Serializer\Annotation\XmlAttribute;
  390. class Input
  391. {
  392. /** @XmlAttributeMap */
  393. private $id = array(
  394. 'name' => 'firstname',
  395. 'value' => 'Adrien',
  396. );
  397. }
  398. Resulting XML:
  399. .. code-block :: xml
  400. <result name="firstname" value="Adrien"/>