@@ -394,24 +394,30 @@ Ignoring Attributes
394
394
395
395
.. note ::
396
396
397
- Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
398
- method is considered best practice.
397
+ Using :ref: `attribute groups <component-serializer-attributes-groups >` is considered best practice.
399
398
400
- As an option, there's a way to ignore attributes from the origin object. To remove
401
- those attributes use the
402
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
403
- method on the normalizer definition::
399
+ As an option, there's a way to ignore attributes from the origin object.
400
+ To remove those attributes provide an array via the ``ignored_attributes ``
401
+ key in the ``context `` parameter of the desired serializer method::
404
402
403
+ use Acme\Person;
405
404
use Symfony\Component\Serializer\Serializer;
406
405
use Symfony\Component\Serializer\Encoder\JsonEncoder;
407
406
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
408
407
408
+ $person = new Person();
409
+ $person->setName('foo');
410
+ $person->setAge(99);
411
+
409
412
$normalizer = new ObjectNormalizer();
410
- $normalizer->setIgnoredAttributes(['age']);
411
413
$encoder = new JsonEncoder();
412
414
413
415
$serializer = new Serializer([$normalizer], [$encoder]);
414
- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
416
+ $serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}
417
+
418
+ .. deprecated :: 4.2
419
+
420
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes ` method was deprecated in Symfony 4.2.
415
421
416
422
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
417
423
0 commit comments