@@ -399,26 +399,30 @@ As for groups, attributes can be selected during both the serialization and dese
399
399
Ignoring Attributes
400
400
-------------------
401
401
402
- .. note ::
403
-
404
- Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
405
- method is considered best practice.
406
-
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::
402
+ As an option, there's a way to ignore attributes from the origin object.
403
+ To remove those attributes provide an array via the ``ignored_attributes ``
404
+ key in the ``context `` parameter of the desired serializer method::
411
405
406
+ use Acme\Person;
412
407
use Symfony\Component\Serializer\Serializer;
413
408
use Symfony\Component\Serializer\Encoder\JsonEncoder;
414
409
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
415
410
411
+ $person = new Person();
412
+ $person->setName('foo');
413
+ $person->setAge(99);
414
+
416
415
$normalizer = new ObjectNormalizer();
417
- $normalizer->setIgnoredAttributes(['age']);
418
416
$encoder = new JsonEncoder();
419
417
420
418
$serializer = new Serializer([$normalizer], [$encoder]);
421
- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
419
+ $serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}
420
+
421
+ .. deprecated :: 4.2
422
+
423
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
424
+ method that was used as an alternative to the ``ignored_attributes `` option
425
+ was deprecated in Symfony 4.2.
422
426
423
427
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
424
428
0 commit comments