Skip to content

Commit

Permalink
minor symfony#11067 Update "Ignoring Attributes" (brucep)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.2 branch (closes symfony#11067).

Discussion
----------

Update "Ignoring Attributes"

The `setIgnoredAttributes()` method has been [deprecated](https://github.com/symfony/serializer/blob/4.2/Normalizer/AbstractNormalizer.php#L181).

Commits
-------

579ad57 Update \"Ignoring Attributes\"
  • Loading branch information
javiereguiluz committed Apr 5, 2019
2 parents 45cb5f2 + 579ad57 commit 9a42530
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,24 +401,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

0 comments on commit 9a42530

Please sign in to comment.