Skip to content

Commit 579ad57

Browse files
Bruce Phillipsjaviereguiluz
Bruce Phillips
authored andcommitted
Update \"Ignoring Attributes\"
1 parent 7d89756 commit 579ad57

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

components/serializer.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,24 +394,30 @@ Ignoring Attributes
394394

395395
.. note::
396396

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.
399398

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::
404402

403+
use Acme\Person;
405404
use Symfony\Component\Serializer\Serializer;
406405
use Symfony\Component\Serializer\Encoder\JsonEncoder;
407406
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
408407

408+
$person = new Person();
409+
$person->setName('foo');
410+
$person->setAge(99);
411+
409412
$normalizer = new ObjectNormalizer();
410-
$normalizer->setIgnoredAttributes(['age']);
411413
$encoder = new JsonEncoder();
412414

413415
$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.
415421

416422
.. _component-serializer-converting-property-names-when-serializing-and-deserializing:
417423

0 commit comments

Comments
 (0)