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

Létrehozva 4y | 2021. máj. 10. 7:20:09


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

SymfonyOnline June 2025: FormFlow: Build Stunning Multistep Forms

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

2025. ápr. 23. 15:20:21 | Symfony
New in Symfony 7.3: Assets Pre-Compression

Contributed by Kévin Dunglas in

2025. ápr. 23. 8:20:31 | Symfony
SymfonyOnline June 2025: Inside a Financial App Breach: Debugging a Million-Dollar Bug

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

2025. ápr. 22. 13:50:03 | Symfony
New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

2025. ápr. 22. 9:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

2025. ápr. 20. 8:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in 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

2025. ápr. 16. 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

2025. ápr. 15. 14:50:24 | Symfony