Skip to content

Update "Ignoring Attributes" #11067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,24 +394,30 @@ Ignoring Attributes

.. note::

Using attribute groups instead of the :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
method is considered best practice.
Using :ref:`attribute groups <component-serializer-attributes-groups>` is considered best practice.

As an option, there's a way to ignore attributes from the origin object. To remove
those attributes use the
:method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
method on the normalizer definition::
As an option, there's a way to ignore attributes from the origin object.
To remove those attributes provide an array via the ``ignored_attributes``
key in the ``context`` parameter of the desired serializer method::

use Acme\Person;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

$person = new Person();
$person->setName('foo');
$person->setAge(99);

$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes(['age']);
$encoder = new JsonEncoder();

$serializer = new Serializer([$normalizer], [$encoder]);
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}
$serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"}

.. deprecated:: 4.2

The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` method was deprecated in Symfony 4.2.

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

Expand Down