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 | useSymfony\Component\Serializer\Annotation\Ignore;classSomeClass{public$someProperty;/** * @Ignore() */public$anotherProperty;private$lastProperty;/** * @Ignore() */publicfunctiongetLastProperty(){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:truelastProperty:ignore:true |
1 2 3 4 5 | <classname="App\SomePath\SomeClass"><!-- ... --><attributename="anotherProperty"ignore="true"/><attributename="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 | useSymfony\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 | useSymfony\Component\Serializer\Encoder\JsonEncoder;useSymfony\Component\Serializer\Normalizer\ArrayDenormalizer;useSymfony\Component\Serializer\Serializer;$serializer=newSerializer([],['json'=>newJsonEncoder()]);'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=newSerializer([newArrayDenormalizer()],['json'=>newJsonEncoder()]);[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') |