From 3a2972a3f413f344d2c0aa8177d850873de619d1 Mon Sep 17 00:00:00 2001 From: Bruce Phillips Date: Wed, 27 Feb 2019 19:23:26 -0500 Subject: [PATCH 1/2] Update "Ignoring Attributes" --- components/serializer.rst | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index a7c2b00c221..10d01837551 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -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 ` 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"} + +.. versionadded:: 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: From 5ef79c829040660335c21d8cb0bf5fcbb291e50c Mon Sep 17 00:00:00 2001 From: Bruce Phillips Date: Sun, 31 Mar 2019 17:24:55 -0400 Subject: [PATCH 2/2] Changes as per review --- components/serializer.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 10d01837551..1db451b7e78 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -398,7 +398,7 @@ Ignoring Attributes 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:: +key in the ``context`` parameter of the desired serializer method:: use Acme\Person; use Symfony\Component\Serializer\Serializer; @@ -415,7 +415,7 @@ key in the ``context``` parameter of the desired serializer method:: $serializer = new Serializer([$normalizer], [$encoder]); $serializer->serialize($person, 'json', ['ignored_attributes' => 'age']); // Output: {"name":"foo"} -.. versionadded:: 4.2 +.. deprecated:: 4.2 The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` method was deprecated in Symfony 4.2.