Annotations ----------- @ExclusionPolicy ~~~~~~~~~~~~~~~~ This annotation can be defined on a class to indicate the exclusion strategy that should be used for the class. +----------+----------------------------------------------------------------+ | Policy | Description | +==========+================================================================+ | all | all properties are excluded by default; only properties marked | | | with @Expose will be serialized/unserialized | +----------+----------------------------------------------------------------+ | none | no properties are excluded by default; all properties except | | | those marked with @Exclude will be serialized/unserialized | +----------+----------------------------------------------------------------+ @Exclude ~~~~~~~~ This annotation can be defined on a property to indicate that the property should not be serialized/unserialized. Works only in combination with NoneExclusionPolicy. If the ``ExpressionLanguageExclusionStrategy`` exclusion strategy is enabled, will be possible to use ``@Exclude(if="expression")`` to exclude dynamically a property. @Expose ~~~~~~~ This annotation can be defined on a property to indicate that the property should be serialized/unserialized. Works only in combination with AllExclusionPolicy. If the ``ExpressionLanguageExclusionStrategy`` exclusion strategy is enabled, will be possible to use ``@Expose(if="expression")`` to expose dynamically a property. @SkipWhenEmpty ~~~~~~~~~~~~~~ This annotation can be defined on a property to indicate that the property should not be serialized if the result will be "empty". Works option works only when serializing. @SerializedName ~~~~~~~~~~~~~~~ This annotation can be defined on a property to define the serialized name for a property. If this is not defined, the property will be translated from camel-case to a lower-cased underscored name, e.g. camelCase -> camel_case. @Since ~~~~~~ This annotation can be defined on a property to specify starting from which version this property is available. If an earlier version is serialized, then this property is excluded automatically. The version must be in a format that is understood by PHP's ``version_compare`` function. @Until ~~~~~~ This annotation can be defined on a property to specify until which version this property was available. If a later version is serialized, then this property is excluded automatically. The version must be in a format that is understood by PHP's ``version_compare`` function. @Groups ~~~~~~~ This annotation can be defined on a property to specify if the property should be serialized when only serializing specific groups (see :doc:`../cookbook/exclusion_strategies`). @MaxDepth ~~~~~~~~~ This annotation can be defined on a property to limit the depth to which the content will be serialized. It is very useful when a property will contain a large object graph. @AccessType ~~~~~~~~~~~ This annotation can be defined on a property, or a class to specify in which way the properties should be accessed. By default, the serializer will retrieve, or set the value via reflection, but you may change this to use a public method instead: .. code-block :: php name; } public function setName($name) { $this->name = trim($name); } } @Accessor ~~~~~~~~~ This annotation can be defined on a property to specify which public method should be called to retrieve, or set the value of the given property: .. code-block :: php name); } public function setName($name) { $this->name = $name; } } .. note :: If you need only to serialize your data, you can avoid providing a setter by setting the property as read-only using the ``@ReadOnly`` annotation. @AccessorOrder ~~~~~~~~~~~~~~ This annotation can be defined on a class to control the order of properties. By default the order is undefined, but you may change it to either "alphabetical", or "custom". .. code-block :: php lastName; } public function getFirstName() { return $this->firstName; } } In this example: - ``id`` is exposed using the object reflection. - ``lastName`` is exposed using the ``getLastName`` getter method. - ``firstName`` is exposed using the ``object.getFirstName()`` expression (``exp`` can contain any valid symfony expression). .. note :: This only works for serialization and is completely ignored during deserialization. @Inline ~~~~~~~ This annotation can be defined on a property to indicate that the data of the property should be inlined. **Note**: This only works for serialization, the serializer will not be able to deserialize objects with this annotation. Also, AccessorOrder will be using the name of the property to determine the order. @ReadOnly ~~~~~~~~~ This annotation can be defined on a property to indicate that the data of the property is read only and cannot be set during deserialization. A property can be marked as non read only with ``@ReadOnly(false)`` annotation (useful when a class is marked as read only). @PreSerialize ~~~~~~~~~~~~~ This annotation can be defined on a method which is supposed to be called before the serialization of the object starts. @PostSerialize ~~~~~~~~~~~~~~ This annotation can be defined on a method which is then called directly after the object has been serialized. @PostDeserialize ~~~~~~~~~~~~~~~~ This annotation can be defined on a method which is supposed to be called after the object has been deserialized. @HandlerCallback ~~~~~~~~~~~~~~~~ This annotation can be defined on a method if serialization/deserialization is handled by the object itself. .. code-block :: php | A list of type T (T can be any available type). | | | Examples: | | | array, array, etc. | +-------------------------------------+--------------------------------------------------+ | array | A map of keys of type K to values of type V. | | | Examples: array, | | | array, etc. | +-------------------------------------+--------------------------------------------------+ | DateTime | PHP's DateTime object (default format/timezone) | +-------------------------------------+--------------------------------------------------+ | DateTime<'format'> | PHP's DateTime object (custom format/default | | | timezone) | +-------------------------------------+--------------------------------------------------+ | DateTime<'format', 'zone'> | PHP's DateTime object (custom format/timezone) | +-------------------------------------+--------------------------------------------------+ | DateTimeImmutable | PHP's DateTimeImmutable object (default format/ | | | timezone) | +-------------------------------------+--------------------------------------------------+ | DateTimeImmutable<'format'> | PHP's DateTimeImmutable object (custom format/ | | | default timezone) | +-------------------------------------+--------------------------------------------------+ | DateTimeImmutable<'format', 'zone'> | PHP's DateTimeImmutable object (custom format/ | | | timezone) | +-------------------------------------+--------------------------------------------------+ | DateInterval | PHP's DateInterval object using ISO 8601 format | +-------------------------------------+--------------------------------------------------+ | T | Where T is a fully qualified class name. | +-------------------------------------+--------------------------------------------------+ | ArrayCollection | Similar to array, but will be deserialized | | | into Doctrine's ArrayCollection class. | +-------------------------------------+--------------------------------------------------+ | ArrayCollection | Similar to array, but will be deserialized | | | into Doctrine's ArrayCollection class. | +-------------------------------------+--------------------------------------------------+ Examples: .. code-block :: php ") */ private $comments; /** * @Type("string") */ private $title; /** * @Type("MyNamespace\Author") */ private $author; /** * @Type("DateTime") */ private $startAt; /** * @Type("DateTime<'Y-m-d'>") */ private $endAt; /** * @Type("DateTimeImmutable") */ private $createdAt; /** * @Type("DateTimeImmutable<'Y-m-d'>") */ private $updatedAt; /** * @Type("boolean") */ private $published; /** * @Type("array") */ private $keyValueStore; } @XmlRoot ~~~~~~~~ This allows you to specify the name of the top-level element. .. code-block :: php .. note :: @XmlRoot only applies to the root element, but is for example not taken into account for collections. You can define the entry name for collections using @XmlList, or @XmlMap. @XmlAttribute ~~~~~~~~~~~~~ This allows you to mark properties which should be set as attributes, and not as child elements. .. code-block :: php @XmlDiscriminator ~~~~~~~~~~~~~~~~~ This annotation allows to modify the behaviour of @Discriminator regarding handling of XML. Available Options: +-------------------------------------+--------------------------------------------------+ | Type | Description | +=====================================+==================================================+ | attribute | use an attribute instead of a child node | +-------------------------------------+--------------------------------------------------+ | cdata | render child node content with or without cdata | +-------------------------------------+--------------------------------------------------+ | namespace | render child node using the specified namespace | +-------------------------------------+--------------------------------------------------+ Example for "attribute": .. code-block :: php Example for "cdata": .. code-block :: php car @XmlValue ~~~~~~~~~ This allows you to mark properties which should be set as the value of the current element. Note that this has the limitation that any additional properties of that object must have the @XmlAttribute annotation. XMlValue also has property cdata. Which has the same meaning as the one in XMLElement. .. code-block :: php 1.23 @XmlList ~~~~~~~~ This allows you to define several properties of how arrays should be serialized. This is very similar to @XmlMap, and should be used if the keys of the array are not important. .. code-block :: php text = $text; } } Resulting XML: .. code-block :: xml You can also specify the entry tag namespace using the ``namespace`` attribute (``@XmlList(inline = true, entry = "comment", namespace="http://www.example.com/ns")``). @XmlMap ~~~~~~~ Similar to @XmlList, but the keys of the array are meaningful. @XmlKeyValuePairs ~~~~~~~~~~~~~~~~~ This allows you to use the keys of an array as xml tags. .. note :: When a key is an invalid xml tag name (e.g. 1_foo) the tag name *entry* will be used instead of the key. @XmlAttributeMap ~~~~~~~~~~~~~~~~ This is similar to the @XmlKeyValuePairs, but instead of creating child elements, it creates attributes. .. code-block :: php 'firstname', 'value' => 'Adrien', ); } Resulting XML: .. code-block :: xml @XmlElement ~~~~~~~~~~~ This annotation can be defined on a property to add additional xml serialization/deserialization properties. .. code-block :: php my_id @XmlNamespace ~~~~~~~~~~~~~ This annotation allows you to specify Xml namespace/s and prefix used. .. code-block :: php