@@ -413,8 +413,75 @@ As for groups, attributes can be selected during both the serialization and dese
413
413
Ignoring Attributes
414
414
-------------------
415
415
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
+ bar :
447
+ ignore : true
448
+
449
+ .. code-block :: xml
450
+
451
+ <?xml version =" 1.0" ?>
452
+ <serializer xmlns =" http://symfony.com/schema/dic/serializer-mapping"
453
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
454
+ xsi : schemaLocation =" http://symfony.com/schema/dic/serializer-mapping
455
+ https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
456
+ >
457
+ <class name =" App\Model\MyClass" >
458
+ <attribute name =" bar" >
459
+ <ignore >true</ignore >
460
+ </attribute >
461
+ </class >
462
+ </serializer >
463
+
464
+ You are now able to ignore specific attributes during serialization::
465
+
466
+ use App\Model\MyClass;
467
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
468
+ use Symfony\Component\Serializer\Serializer;
469
+
470
+ $obj = new MyClass();
471
+ $obj->foo = 'foo';
472
+ $obj->bar = 'bar';
473
+
474
+ $normalizer = new ObjectNormalizer($classMetadataFactory);
475
+ $serializer = new Serializer([$normalizer]);
476
+
477
+ $data = $serializer->normalize($obj);
478
+ // $data = ['foo' => 'foo'];
479
+
480
+
481
+ Option 2: Using the context
482
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
483
+
484
+ By providing an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES ``
418
485
key in the ``context `` parameter of the desired serializer method::
419
486
420
487
use Acme\Person;
0 commit comments