Contributed by Maxime Steinhausser in #39399.
The Serializer component provides a mechanism to configure the way data is serialized/deserialized: the context options. These options are passed as an optional argument of the serialize/deserialize methods. In Symfony 5.3 we’ve improved the serializer so you can define the context as part of the mapping information thanks to a new Serializer\Context annotation/attribute. For example, to define how some datetime property should be formatted, use the following: 1 2 3 4 5 6 7 8 9 10 11 12 13 14use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
class SomeClass { /**
-
@Serializer\Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' }) */ public \DateTime $date;
// In PHP 8 applications you can use PHP attributes instead:
[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public \DateTime $date; }
The annotation/attribute supports different contexts for normalization/denormalization and it can also define serialization groups: 1 2 3 4 5 6 7 8 9 10 11#[Serializer\Context( normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'], denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::COOKIE] )] public \DateTime $date;
[Serializer\Context(
normalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339],
groups: ['extended']
)] public \DateTime $date;
Sponsor the Symfony project.
Jelentkezéshez jelentkezzen be
EGYÉB POSTS Ebben a csoportban

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.
Al

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.
We’re thrilled to announce

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.
Al

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.
We’re thrilled to announce

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.
Al