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

Creato 4y | 10 mag 2021, 07:20:09


Accedi per aggiungere un commento

Altri post in questo gruppo

New in Symfony 7.3: Simpler Server Event Streaming

Contributed by Yonel Ceruto in

5 mag 2025, 07:50:03 | Symfony
A Week of Symfony #957 (April 28 – May 4, 2025)

This week, Symfony 6.4.21 and 7.2.6 maintenance versions were released. In addition, we published the first beta of Symfony 7.3 so you can test it in your own apps before its final release at the end

4 mag 2025, 08:40:05 | Symfony
Symfony 7.2.6 released

Symfony 7.2.6 has just been released. Here is the list of the most important changes since 7.2.5:

bug #60288 [VarExporter] dump default value for property hooks if present (@xabbuh)

bug #60267 [C

2 mag 2025, 12:40:05 | Symfony
Symfony 7.3.0-BETA1 released

Symfony 7.3.0-BETA1 has just been released. Here is the list of the most important changes since 7.2:

feature #60232 Add PHP config support for routing (@fabpot)

feature #60102 [HttpFoundation] A

2 mag 2025, 12:40:04 | Symfony
SymfonyOnline June 2025 : Efficient Web Scraping with Symfony & PHP

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

2 mag 2025, 12:40:03 | Symfony
New in Symfony 7.3: Configurable Compound Rate Limiter

Contributed by Kevin Bond in

2 mag 2025, 10:20:09 | Symfony
Symfony 6.4.21 released

Symfony 6.4.21 has just been released. Here is the list of the most important changes since 6.4.20:

bug #60288 [VarExporter] dump default value for property hooks if present (@xabbuh)

bug #60268

2 mag 2025, 10:20:08 | Symfony