@@ -401,24 +401,30 @@ Ignoring Attributes
401
401
402
402
.. note ::
403
403
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.
406
405
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::
411
409
410
+ use Acme\Person;
412
411
use Symfony\Component\Serializer\Serializer;
413
412
use Symfony\Component\Serializer\Encoder\JsonEncoder;
414
413
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
415
414
415
+ $person = new Person();
416
+ $person->setName('foo');
417
+ $person->setAge(99);
418
+
416
419
$normalizer = new ObjectNormalizer();
417
- $normalizer->setIgnoredAttributes(['age']);
418
420
$encoder = new JsonEncoder();
419
421
420
422
$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.
422
428
423
429
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
424
430
0 commit comments