Symfony 7.3 adds a new JsonStreamer component as a high-performance, low-memory JSON encoding and decoding utility. However, the Serializer component still has many valid use cases, even for JSON content, thanks to its rich feature set and flexibility. In Symfony 7.3, we've improved it with several new features.

New Number Normalizer

Valtteri R
Contributed by Valtteri R in #59670

The Serializer component provides many built-in normalizers to transform various data types. In Symfony 7.3, we're adding a new NumberNormalizer that converts between BcMath\Number or GMP objects and their representation as strings or integers.

Ignore Empty Attributes

Quentin Dequippe
Contributed by Quentin Dequippe in #58599

The XML encoder that encodes/decodes data in XML format defines many XML context options to configure its behavior. In Symfony 7.3, we're adding a new ignore_empty_attributes option.

When set to true, attributes with empty values will be filtered out from the generated XML content.

Default Type for Discriminator Maps

Alan Poulain
Contributed by Alan Poulain in #59828

Discriminator maps allow you to define the concrete class to use when deserializing properties that refer to interfaces or abstract classes. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace App\Model;

use Symfony\Component\Serializer\Attribute\DiscriminatorMap;

#[DiscriminatorMap(
    typeProperty: 'type',
    mapping: [
        'product' => Product::class,
        'shipping' => Shipping::class,
    ]
)]
interface InvoiceItemInterface
{
    // ...
}

The incoming data (e.g. JSON) must include the type property, which is used to determine the class to instantiate:

1
2
3
4
5
{
    "product": "...",
    "duration": "...",
    "type": "video"
}

In Symfony 7.3, we're improving these maps so you can define a default type to use when the incoming data does not include the type property:

1
2
3
4
5
6
7
8
#[DiscriminatorMap(
typeProperty: 'type',
    mapping: [
        'article' => Article::class,
        'video' => Video::class,
    ],
    defaultType: 'article'
)]

The value defined in defaultType will be used when no type is provided.

Show Discriminator Map in Debug Output

Jan Schädlich
Contributed by Jan Schädlich in #52749

The debug:serializer command provides a quick overview of the serialization information of a given class. In Symfony 7.3, we're improving its output to include discriminator maps, when present:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ php bin/console debug:serializer App\Some\Class

App\Some\Class
-----------------------------------------------------------------------

+----------+------------------------------------------------------------------------+
| Property | Options                                                                |
+----------+------------------------------------------------------------------------+
| type     | [                                                                      |
|          |   "groups" => [],                                                      |
|          |   "maxDepth" => null,                                                  |
|          |   [...]                                                                |
|          |   "discriminatorMap" => [                                              |
|          |     "one" => "App\Some\DiscriminatorMapOne",                           |
|          |     "two" => "App\Some\DiscriminatorMapTwo",                           |
|          |   ]                                                                    |
|          | ]                                                                      |
+----------+------------------------------------------------------------------------+
Published in #Living on the edge