Skip to content

Commit 6a3b355

Browse files
Valentinnoniagriconomie
Valentin
andcommitted
[Serializer] Add an @ignore annotation #28744
Update serializer.rst Update components/serializer.rst Co-authored-by: Oskar Stark <oskarstark@googlemail.com> Update components/serializer.rst Co-authored-by: Antoine Makdessi <antoine.makdessi@agriconomie.com> Update components/serializer.rst Co-authored-by: Antoine Makdessi <antoine.makdessi@agriconomie.com> Update components/serializer.rst Co-authored-by: Oskar Stark <oskarstark@googlemail.com> Update components/serializer.rst Co-authored-by: Antoine Makdessi <antoine.makdessi@agriconomie.com> Update serializer.rst
1 parent 94656d4 commit 6a3b355

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

components/serializer.rst

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,81 @@ As for groups, attributes can be selected during both the serialization and dese
413413
Ignoring Attributes
414414
-------------------
415415

416-
As an option, there's a way to ignore attributes from the origin object.
417-
To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES``
416+
All attributes are included by default when serializing objects. You have two alternatives to ignore some of those attributes.
417+
418+
* `Option 1: Using @Ignore annotation`_
419+
* `Option 2: Using the context`_
420+
421+
Option 1: Using ``@Ignore`` annotation
422+
--------------------------------------
423+
424+
.. configuration-block::
425+
426+
.. code-block:: php-annotations
427+
428+
namespace App\Model;
429+
430+
use Symfony\Component\Serializer\Annotation\Ignore;
431+
432+
class MyClass
433+
{
434+
public $foo;
435+
436+
/**
437+
* @Ignore()
438+
*/
439+
public $bar;
440+
}
441+
442+
.. code-block:: yaml
443+
444+
App\Model\MyClass:
445+
attributes:
446+
foo:
447+
ignore: false
448+
bar:
449+
ignore: true
450+
451+
.. code-block:: xml
452+
453+
<?xml version="1.0" ?>
454+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
455+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
456+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
457+
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
458+
>
459+
<class name="App\Model\MyClass">
460+
<attribute name="foo">
461+
<ignore>false</ignore>
462+
</attribute>
463+
464+
<attribute name="bar">
465+
<ignore>true</ignore>
466+
</attribute>
467+
</class>
468+
</serializer>
469+
470+
You are now able to ignore specific attributes during serialization::
471+
472+
use App\Model\MyClass;
473+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
474+
use Symfony\Component\Serializer\Serializer;
475+
476+
$obj = new MyClass();
477+
$obj->foo = 'foo';
478+
$obj->bar = 'bar';
479+
480+
$normalizer = new ObjectNormalizer($classMetadataFactory);
481+
$serializer = new Serializer([$normalizer]);
482+
483+
$data = $serializer->normalize($obj);
484+
// $data = ['foo' => 'foo'];
485+
486+
487+
Option 2: Using the context
488+
---------------------------
489+
490+
By providing an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES``
418491
key in the ``context`` parameter of the desired serializer method::
419492

420493
use Acme\Person;

0 commit comments

Comments
 (0)