Added @Ignore annotation

Kévin Dunglas
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

Eduard Bulava
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

Kévin Dunglas
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

Alexander Menshchikov
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')
Published in #Living on the edge