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

Creado 4y | 10 may 2021, 7:20:09


Inicia sesión para agregar comentarios

Otros mensajes en este grupo.

Symfony UX CVE-2025-47946: Unsanitized HTML attribute injection via ComponentAttributes

Affected versions

Symfony UX symfony/ux-live-component and symfony/ux-twig-component versions <2.25.1 are affected by this security issue.

The issue has been fixed in the 2.25.1 version of these

19 may 2025, 12:40:14 | Symfony
New in Symfony 7.3: Yaml Improvements

Symfony has been reducing the need for configuration in applications for several years now. Thanks to PHP attributes, you can now configure most things alongside the relevant code, removing the need f

19 may 2025, 8:10:09 | Symfony
A Week of Symfony #959 (May 12–18, 2025)

This week, development activity focused on polishing Symfony 7.3 ahead of its final release in two weeks. We also continued publishing articles highlighting the new features of Symfony 7.3 and shared

18 may 2025, 8:50:08 | Symfony
SymfonyOnline June 2025:  Keynote “Symfony in 2025, Scaling to Zero.”

SymfonyOnline June 2025 is almost here, starting in a few weeks on:

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

16 may 2025, 15:10:26 | Symfony
New in Symfony 7.3: Dependency Injection Improvements

Symfony 7.3 introduces several enhancements to the DependencyInjection component that simplify service configuration, make autoconfiguration more flexible, and enable environment-specific aliasing.

S

16 may 2025, 8:10:11 | Symfony
New in Symfony 7.3: Namespaced Caches

Contributed by Nicolas Grekas in

15 may 2025, 8: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 may 2025, 14:30:11 | Symfony