New in Symfony 5.1: Serializer improvements
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
I guess 'UnwrappingDenormalizer::UNWRAP_PATH' should be unquoted ?
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 ?
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.