New in Symfony 6.4: Serializer Improvements

Class-based Group Attributes

Contributed by
Adrian Brajkovic

in #49594.

Serialization groups allow to serialize different sets of attributes from your
entities according to your needs. A group name is an arbitrary string that associates
a property to one or more of those serialization sets.

In Symfony 6.4 we’re improving serialization groups so you can also define them
at class level. This will associate all properties to that group, in addition
to the groups that each property optionally associates to:

#[ORMEntity]
#[Groups([’show_product‘])]
class Product
{
// …

#[ORMColumn(type: ’string‘, length: 255)]
#[Groups([‚list_product‘])]
private string $name;

#[ORMColumn(type: ‚text‘)]
private string $description;
}

In this example, the name property belongs to show_product and list_product
groups, but the description property only belongs to the show_product group.

Translatable Objects Normalizer

Contributed by
Hubert Lenoir

in #50212.

In Symfony 5.2 we introduced translatable objects, which are objects that
contain all the information needed to translate its own contents, such as the
translation parameters and the translation domain.

In Symfony 6.4, we’re introducing a TranslatableObject normalizer, which
translates the contents of these objects to the locale defined in the NORMALIZATION_LOCALE_KEY
option when serializing objects. There’s nothing to do to use this new normalizer.
Upgrade your application to Symfony 6.4 or higher and this will work out-of-the-box.

Detailed JSON Decoding Errors

Contributed by
Gabriel Ostrolucký

in #51215
and #51172.

Consider the following JSON snippet which contains a certain syntax error:

$jsonContent = „{‚foo‘: ‚bar‘}“;

In current Symfony versions, when trying to deserialize that content, you’ll see
this error:

{
„title“: „Deserialization Failed“,
„detail“: „Syntax error“
}

Not very useful, right? In Symfony 6.4 we’re improving this significantly.
First, install the seld/jsonlint dependency in your project. Then, upgrade
to Symfony 6.4 or higher and, when the application runs in the debug mode,
you’ll see detailed errors like the following:

{
„title“: „Deserialization Failed“,
„detail“: „Parse error on line 1: {‚foo‘: ‚bar‘} Invalid string, it appears you used single quotes instead of double quotes“
}

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.4.1 released!

PHP 8.1.31 released!

PHP 8.3.14 released!

PHP 8.2.26 released!

Generated by Feedzy