@@ -368,6 +368,83 @@ stored in one of the following locations:
368
368
369
369
.. _serializer-enabling-metadata-cache :
370
370
371
+ Using nested attributes
372
+ -----------------------
373
+
374
+ To map nested properties, a ``SerializedPath `` can be defined with annotations,
375
+ attributes and YAML or XML configurations:
376
+
377
+ .. configuration-block ::
378
+
379
+ .. code-block :: php-annotations
380
+
381
+ namespace App\Model;
382
+
383
+ use Symfony\Component\Serializer\Annotation\SerializedPath;
384
+
385
+ class Person
386
+ {
387
+ /**
388
+ *@SerializedPath("[profile][information][birthday]")
389
+ */
390
+ private $dob;
391
+
392
+ // ...
393
+ }
394
+
395
+ .. code-block :: php-attributes
396
+
397
+ namespace App\Model;
398
+
399
+ use Symfony\Component\Serializer\Annotation\SerializedPath;
400
+
401
+ class Person
402
+ {
403
+ #[SerializedPath('[profile][information][birthday]')]
404
+ private $dob;
405
+
406
+ // ...
407
+ }
408
+
409
+ .. code-block :: yaml
410
+
411
+ App\Model\Person :
412
+ attributes :
413
+ dob :
414
+ serialized_path : ' [profile][information][birthday]'
415
+
416
+ .. code-block :: xml
417
+
418
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
419
+ <serializer xmlns =" http://symfony.com/schema/dic/serializer-mapping"
420
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
421
+ xsi : schemaLocation =" http://symfony.com/schema/dic/serializer-mapping
422
+ https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
423
+ >
424
+ <class name =" App\Model\Person" >
425
+ <attribute name =" dob" serialized-path =" [profile][information][birthday]" />
426
+ </class >
427
+ </serializer >
428
+
429
+ Using the configuration from above, denormalizing with a metadata-aware
430
+ normalizer will write the ``birthday `` field from ``$data `` onto the ``Person ``
431
+ object::
432
+
433
+ $data = [
434
+ 'profile' => [
435
+ 'basicinformation' => [
436
+ 'birthday' => '01-01-1970',
437
+ ],
438
+ ],
439
+ ];
440
+ $person = $normalizer->denormalize($data, Person::class, 'any');
441
+ $person->getBirthday() // 01-01-1970
442
+
443
+ When using annotations or attributes, the ``SerializedPath `` can either
444
+ be set on the property or the associated getter. The ``SerializedPath ``
445
+ cannot be used in combination with a ``SerializedName `` for the same propety.
446
+ The given path must be a string that can be parsed as a ``PropertyPath ``.
447
+
371
448
Configuring the Metadata Cache
372
449
------------------------------
373
450
0 commit comments