annotations.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. A property can be marked as non read only with ``@ReadOnly(false)`` annotation (useful when a class is marked as read only).
  160. @PreSerialize
  161. ~~~~~~~~~~~~~
  162. This annotation can be defined on a method which is supposed to be called before
  163. the serialization of the object starts.
  164. @PostSerialize
  165. ~~~~~~~~~~~~~~
  166. This annotation can be defined on a method which is then called directly after the
  167. object has been serialized.
  168. @PostDeserialize
  169. ~~~~~~~~~~~~~~~~
  170. This annotation can be defined on a method which is supposed to be called after
  171. the object has been deserialized.
  172. @HandlerCallback
  173. ~~~~~~~~~~~~~~~~
  174. This annotation can be defined on a method if serialization/deserialization is handled
  175. by the object iself.
  176. .. code-block :: php
  177. <?php
  178. class Article
  179. {
  180. /**
  181. * @HandlerCallback("xml", direction = "serialization")
  182. */
  183. public function serializeToXml(XmlSerializationVisitor $visitor)
  184. {
  185. // custom logic here
  186. }
  187. }
  188. @Discriminator
  189. ~~~~~~~~~~~~~~
  190. .. versionadded : 0.12
  191. @Discriminator was added
  192. This annotation allows deserialization of relations which are polymorphic, but
  193. where a common base class exists. The ``@Discriminator`` annotation has to be applied
  194. to the least super type::
  195. /**
  196. * @Discriminator(field = "type", map = {"car": "Car", "moped": "Moped"})
  197. */
  198. abstract class Vehicle { }
  199. class Car extends Vehicle { }
  200. class Moped extends Vehicle { }
  201. @Type
  202. ~~~~~
  203. This annotation can be defined on a property to specify the type of that property.
  204. For deserialization, this annotation must be defined. For serialization, you may
  205. define it in order to enhance the produced output; for example, you may want to
  206. force a certain format to be used for DateTime types.
  207. Available Types:
  208. +---------------------------+--------------------------------------------------+
  209. | Type | Description |
  210. +===========================+==================================================+
  211. | boolean | Primitive boolean |
  212. +---------------------------+--------------------------------------------------+
  213. | integer | Primitive integer |
  214. +---------------------------+--------------------------------------------------+
  215. | double | Primitive double |
  216. +---------------------------+--------------------------------------------------+
  217. | string | Primitive string |
  218. +---------------------------+--------------------------------------------------+
  219. | array | An array with arbitrary keys, and values. |
  220. +---------------------------+--------------------------------------------------+
  221. | array<T> | A list of type T (T can be any available type). |
  222. | | Examples: |
  223. | | array<string>, array<MyNamespace\MyObject>, etc. |
  224. +---------------------------+--------------------------------------------------+
  225. | array<K, V> | A map of keys of type K to values of type V. |
  226. | | Examples: array<string, string>, |
  227. | | array<string, MyNamespace\MyObject>, etc. |
  228. +---------------------------+--------------------------------------------------+
  229. | DateTime | PHP's DateTime object (default format/timezone) |
  230. +---------------------------+--------------------------------------------------+
  231. | DateTime<'format'> | PHP's DateTime object (custom format/default |
  232. | | timezone) |
  233. +---------------------------+--------------------------------------------------+
  234. | DateTime<'format', 'zone'>| PHP's DateTime object (custom format/timezone) |
  235. +---------------------------+--------------------------------------------------+
  236. | T | Where T is a fully qualified class name. |
  237. +---------------------------+--------------------------------------------------+
  238. | ArrayCollection<T> | Similar to array<T>, but will be deserialized |
  239. | | into Doctrine's ArrayCollection class. |
  240. +---------------------------+--------------------------------------------------+
  241. | ArrayCollection<K, V> | Similar to array<K, V>, but will be deserialized |
  242. | | into Doctrine's ArrayCollection class. |
  243. +---------------------------+--------------------------------------------------+
  244. Examples:
  245. .. code-block :: php
  246. <?php
  247. namespace MyNamespace;
  248. use JMS\Serializer\Annotation\Type;
  249. class BlogPost
  250. {
  251. /**
  252. * @Type("ArrayCollection<MyNamespace\Comment>")
  253. */
  254. private $comments;
  255. /**
  256. * @Type("string")
  257. */
  258. private $title;
  259. /**
  260. * @Type("MyNamespace\Author")
  261. */
  262. private $author;
  263. /**
  264. * @Type("DateTime")
  265. */
  266. private $createdAt;
  267. /**
  268. * @Type("DateTime<'Y-m-d'>")
  269. */
  270. private $updatedAt;
  271. /**
  272. * @Type("boolean")
  273. */
  274. private $published;
  275. /**
  276. * @Type("array<string, string>")
  277. */
  278. private $keyValueStore;
  279. }
  280. @XmlRoot
  281. ~~~~~~~~
  282. This allows you to specify the name of the top-level element.
  283. .. code-block :: php
  284. <?php
  285. use JMS\Serializer\Annotation\XmlRoot;
  286. /** @XmlRoot("user") */
  287. class User
  288. {
  289. private $name = 'Johannes';
  290. }
  291. Resulting XML:
  292. .. code-block :: xml
  293. <user>
  294. <name><![CDATA[Johannes]]></name>
  295. </user>
  296. .. note ::
  297. @XmlRoot only applies to the root element, but is for example not taken into
  298. account for collections. You can define the entry name for collections using
  299. @XmlList, or @XmlMap.
  300. @XmlAttribute
  301. ~~~~~~~~~~~~~
  302. This allows you to mark properties which should be set as attributes,
  303. and not as child elements.
  304. .. code-block :: php
  305. <?php
  306. use JMS\Serializer\Annotation\XmlAttribute;
  307. class User
  308. {
  309. /** @XmlAttribute */
  310. private $id = 1;
  311. private $name = 'Johannes';
  312. }
  313. Resulting XML:
  314. .. code-block :: xml
  315. <result id="1">
  316. <name><![CDATA[Johannes]]></name>
  317. </result>
  318. @XmlValue
  319. ~~~~~~~~~
  320. This allows you to mark properties which should be set as the value of the
  321. current element. Note that this has the limitation that any additional
  322. properties of that object must have the @XmlAttribute annotation.
  323. XMlValue also has property cdata. Which has the same meaning as the one in
  324. XMLElement.
  325. .. code-block :: php
  326. <?php
  327. use JMS\Serializer\Annotation\XmlAttribute;
  328. use JMS\Serializer\Annotation\XmlValue;
  329. use JMS\Serializer\Annotation\XmlRoot;
  330. /** @XmlRoot("price") */
  331. class Price
  332. {
  333. /** @XmlAttribute */
  334. private $currency = 'EUR';
  335. /** @XmlValue */
  336. private $amount = 1.23;
  337. }
  338. Resulting XML:
  339. .. code-block :: xml
  340. <price currency="EUR">1.23</price>
  341. @XmlList
  342. ~~~~~~~~
  343. This allows you to define several properties of how arrays should be
  344. serialized. This is very similar to @XmlMap, and should be used if the
  345. keys of the array are not important.
  346. .. code-block :: php
  347. <?php
  348. use JMS\Serializer\Annotation\XmlList;
  349. use JMS\Serializer\Annotation\XmlRoot;
  350. /** @XmlRoot("post") */
  351. class Post
  352. {
  353. /**
  354. * @XmlList(inline = true, entry = "comment")
  355. */
  356. private $comments = array(
  357. new Comment('Foo'),
  358. new Comment('Bar'),
  359. );
  360. }
  361. class Comment
  362. {
  363. private $text;
  364. public function __construct($text)
  365. {
  366. $this->text = $text;
  367. }
  368. }
  369. Resulting XML:
  370. .. code-block :: xml
  371. <post>
  372. <comment>
  373. <text><![CDATA[Foo]]></text>
  374. </comment>
  375. <comment>
  376. <text><![CDATA[Bar]]></text>
  377. </comment>
  378. </post>
  379. @XmlMap
  380. ~~~~~~~
  381. Similar to @XmlList, but the keys of the array are meaningful.
  382. @XmlKeyValuePairs
  383. ~~~~~~~~~~~~~~~~~
  384. This allows you to use the keys of an array as xml tags.
  385. .. note ::
  386. When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key.
  387. @XmlAttributeMap
  388. ~~~~~~~~~~~~~~~~
  389. This is similar to the @XmlKeyValuePairs, but instead of creating child elements, it creates attributes.
  390. .. code-block :: php
  391. <?php
  392. use JMS\Serializer\Annotation\XmlAttribute;
  393. class Input
  394. {
  395. /** @XmlAttributeMap */
  396. private $id = array(
  397. 'name' => 'firstname',
  398. 'value' => 'Adrien',
  399. );
  400. }
  401. Resulting XML:
  402. .. code-block :: xml
  403. <result name="firstname" value="Adrien"/>
  404. @XmlElement
  405. ~~~~~~~~~~~
  406. This annotation can be defined on a property to add additional xml serialization/deserialization properties.
  407. .. code-block :: php
  408. <?php
  409. use JMS\Serializer\Annotation\XmlElement;
  410. /**
  411. * @XmlNamespace(uri="http://www.w3.org/2005/Atom", prefix="atom")
  412. */
  413. class User
  414. {
  415. /**
  416. * @XmlElement(cdata=false, namespace="http://www.w3.org/2005/Atom")
  417. */
  418. private $id = 'my_id';
  419. }
  420. Resulting XML:
  421. .. code-block :: xml
  422. <atom:id>my_id</atom:id>
  423. @XmlNamespace
  424. ~~~~~~~~~~~~~
  425. This annotation allows you to specify Xml namespace/s and prefix used.
  426. .. code-block :: php
  427. <?php
  428. use JMS\Serializer\Annotation\XmlNamespace;
  429. /**
  430. * @XmlNamespace(uri="http://example.com/namespace")
  431. * @XmlNamespace(uri="http://www.w3.org/2005/Atom", prefix="atom")
  432. */
  433. class BlogPost
  434. {
  435. /**
  436. * @Type("JMS\Serializer\Tests\Fixtures\Author")
  437. * @Groups({"post"})
  438. * @XmlElement(namespace="http://www.w3.org/2005/Atom")
  439. */
  440. private $author;
  441. }
  442. class Author
  443. {
  444. /**
  445. * @Type("string")
  446. * @SerializedName("full_name")
  447. */
  448. private $name;
  449. }
  450. Resulting XML:
  451. .. code-block :: xml
  452. <?xml version="1.0" encoding="UTF-8"?>
  453. <blog-post xmlns="http://example.com/namespace" xmlns:atom="http://www.w3.org/2005/Atom">
  454. <atom:author>
  455. <full_name><![CDATA[Foo Bar]]></full_name>
  456. </atom:author>
  457. </blog>