diff --git a/components/serializer.rst b/components/serializer.rst index 1690959defa..b6e5498d31b 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -229,6 +229,11 @@ normalized data, instead of the denormalizer re-creating them. Note that arrays of objects. Those will still be replaced when present in the normalized data. +Context +------- + +Many Serializer features can be configured :doc:`using a context `. + .. _component-serializer-attributes-groups: Attributes Groups diff --git a/serializer.rst b/serializer.rst index ab3e8b2002f..cc98e64e66e 100644 --- a/serializer.rst +++ b/serializer.rst @@ -172,6 +172,112 @@ configuration: The ability to configure the ``default_context`` option in the Serializer was introduced in Symfony 5.4. +You can also specify the context on a per-property basis:: + +.. configuration-block:: + + .. code-block:: php-annotations + + namespace App\Model; + + use Symfony\Component\Serializer\Annotation\Context; + use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; + + class Person + { + /** + * @Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' }) + */ + public $createdAt; + + // ... + } + + .. code-block:: php-attributes + + namespace App\Model; + + use Symfony\Component\Serializer\Annotation\Context; + use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; + + class Person + { + #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])] + public $createdAt; + + // ... + } + + .. code-block:: yaml + + App\Model\Person: + attributes: + createdAt: + context: + datetime_format: 'Y-m-d' + + .. code-block:: xml + + + + + + + Y-m-d + + + + + +Use the options to specify context specific to normalization or denormalization:: + + namespace App\Model; + + use Symfony\Component\Serializer\Annotation\Context; + use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; + + class Person + { + #[Context( + normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'], + denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339], + )] + public $createdAt; + + // ... + } + +You can also restrict the usage of a context to some groups:: + + namespace App\Model; + + use Symfony\Component\Serializer\Annotation\Context; + use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; + + class Person + { + #[Serializer\Groups(['extended'])] + #[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])] + #[Serializer\Context( + context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED], + groups: ['extended'], + )] + public $createdAt; + + // ... + } + +The attribute/annotation can be repeated as much as needed on a single property. +Context without group is always applied first. Then context for the matching groups are merged in the provided order. + +.. versionadded:: 5.3 + + The ``Context`` attribute, annotation and the configuration options were introduced in Symfony 5.3. + .. _serializer-using-serialization-groups-annotations: Using Serialization Groups Annotations pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy