New in Symfony 5.3: Inlined Serialization Context

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.

http://feedproxy.google.com/~r/symfony/blog/~3/ZHkZkGEe9MU/new-in-symfony-5-3-inlined-serialization-context

созданный 4y | 10 мая 2021 г., 07:20:09


Войдите, чтобы добавить комментарий

Другие сообщения в этой группе

New in Symfony 7.3: Namespaced Caches

Contributed by Nicolas Grekas in

15 мая 2025 г., 08:50:14 | Symfony
SymfonyOnline June 2025: Building an Automated Trading System with Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

14 мая 2025 г., 14:30:11 | Symfony
New in Symfony 7.3: Security Improvements

Symfony security includes several significant improvements and new features in Symfony 7.3.

Deprecate eraseCredentials() Method… https://symfony.com/blog/new-in-symfony-7-3-security-improvements?utm_

14 мая 2025 г., 09:50:05 | Symfony
SymfonyOnline June 2025: Demystify the Magic of the Container🪄

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days June 12-13: Online conference days in English. All talks will be available for replay as soon

13 мая 2025 г., 15:20:17 | Symfony
New in Symfony 7.3: ObjectMapper Component

Contributed by Antoine Bluchet in

13 мая 2025 г., 08:20:10 | Symfony
New in Symfony 7.3: Explaining Security Voter Decisions

Contributed by Nicolas Grekas in

12 мая 2025 г., 09:10:10 | Symfony
A Week of Symfony #958 (May 5–11, 2025)

This week, we published the second beta of Symfony 7.3, ahead of its final release later in May 2025. Meanwhile, we continued sharing posts about the new features of Symfony 7.3 and the upcoming Symfo

11 мая 2025 г., 09:50:17 | Symfony