Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 5.1: Serializer improvements

Added @Ignore annotation

Contributed by
Kévin Dunglas
in #28744.

Symfony 5.1 adds a new @Ignore annotation to allow ignoring some values when serializing. You can apply the annotation both to properties and methods. Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
use Symfony\Component\Serializer\Annotation\Ignore;

class SomeClass
{
    public $someProperty;
    /**
     * @Ignore()
     */
    public $anotherProperty;
    private $lastProperty;

    /**
     * @Ignore()
     */
    public function getLastProperty()
    {
        return $this->lastProperty;
    }
}

This is also available in YAML and XML formats using the ignore option:

1
2
3
4
5
6
7
App\SomePath\SomeClas:
    attributes:
        # ...
        anotherProperty:
            ignore: true
        lastProperty:
            ignore: true
1
2
3
4
5
<class name="App\SomePath\SomeClass">
    <!-- ... -->
    <attribute name="anotherProperty" ignore="true" />
    <attribute name="lastProperty" ignore="true" />
</class>

Unwrapping Denormalizer

Contributed by
Eduard Bulava
in #31390.

APIs often return nested responses in which you only need some child object. In Symfony 5.1, thanks to the new UnwrappingDenormalizer, you can get any nested object without creating unnecessary model classes:

1
2
3
4
5
6
7
8
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;

$result = $serialiser->deserialize(
    '{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}',
    Object::class,
    [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]']
);
// $result->title === 'value'

Added support for stdClass

Contributed by
Kévin Dunglas
in #35596.

When an object contains properties of PHP stdClass, serialization fails. In Symfony 5.1 we've added support for it:

1
2
3
4
5
$object = new \stdClass();
$object->foo = 'f';
$object->bar = 'b';

$normalizer->normalize($object) === ['foo' => 'f', 'bar' => 'b']

Scalar denormalization

Contributed by
Alexander Menshchikov
in #35235.

In Symfony 5.1 we also added support for scalar values denormalization. These scalar values are numbers (int or float), booleans and strings. The following example shows how can you normalize and denormalize those values:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Serializer;

$serializer = new Serializer([], ['json' => new JsonEncoder()]);

'42' === $serializer->serialize(42, 'json')
'true' === $serializer->serialize(true, 'json')
'3.14' === $serializer->serialize(3.14, 'json')
'foo bar' === $serializer->serialize('foo bar', 'json')

$serializer = new Serializer(
    [new ArrayDenormalizer()],
    ['json' => new JsonEncoder()]
);

[42] === $serializer->deserialize('[42]', 'int[]', 'json')
[true, false] === $serializer->deserialize('[true,false]', 'bool[]', 'json')
[3.14] === $serializer->deserialize('[3.14]', 'float[]', 'json')
['foo bar'] === $serializer->deserialize('["foo bar"]', 'string[]', 'json')
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

Great improvements !

I guess 'UnwrappingDenormalizer::UNWRAP_PATH' should be unquoted ?
Great stuff !
Although I wonder about the scalar denormalization. The title says denormalization but in fact it's a deserialize. I don't see the added value of calling it deserialize when it is just a denormalization. Or am I missing something ?
normalize => object to array representation
serialize => object to array to JSON string representation
deserialize => JSON string representation to array to object
denormalize => map array representation to object

I guess it is called "UnwrappingDenormalizer", although the "deserialize" method is used, because the JSON is completely deserialized (to array), but only the specified path is then deNORMALIZED to object.
Is it possible to use the @Ignore annotation together with groups? So that property or method is only ignored on defined groups?
The unwrapping denormalizer will be perfect for a soap implementation where the envelope would have to be a new class for each call, which can now be omitted!
@Jibé you are right! I've just fixed it. Thanks!
I was literally looking for something like @ignore last week. Great addition!
Nice!!! Thanks!!!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.