annotations.rst 14 KB

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