@@ -401,24 +401,30 @@ Ignoring Attributes
401401
402402.. note ::
403403
404- Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
405- method is considered best practice.
404+ Using :ref: `attribute groups <component-serializer-attributes-groups >` is considered best practice.
406405
407- As an option, there's a way to ignore attributes from the origin object. To remove
408- those attributes use the
409- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
410- method on the normalizer definition::
406+ As an option, there's a way to ignore attributes from the origin object.
407+ To remove those attributes provide an array via the ``ignored_attributes ``
408+ key in the ``context `` parameter of the desired serializer method::
411409
410+ use Acme\Person;
412411 use Symfony\Component\Serializer\Serializer;
413412 use Symfony\Component\Serializer\Encoder\JsonEncoder;
414413 use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
415414
415+ $person = new Person();
416+ $person->setName('foo');
417+ $person->setAge(99);
418+
416419 $normalizer = new ObjectNormalizer();
417- $normalizer->setIgnoredAttributes(['age']);
418420 $encoder = new JsonEncoder();
419421
420422 $serializer = new Serializer([$normalizer], [$encoder]);
421- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
423+ $serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}
424+
425+ .. deprecated :: 4.2
426+
427+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes ` method was deprecated in Symfony 4.2.
422428
423429.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
424430
0 commit comments