Skip to content

Commit cb83dfc

Browse files
committed
minor #17396 [Serializer] SerializedPath documentation (boenner)
This PR was merged into the 6.2 branch. Discussion ---------- [Serializer] SerializedPath documentation Adds documentation for the `SerializedPath` annotation (symfony/symfony#43534). This fixes #17389. Commits ------- 375c089 Documentation for the SerializedPath definition
2 parents 88e9b4a + 375c089 commit cb83dfc

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

serializer.rst

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,87 @@ stored in one of the following locations:
368368

369369
.. _serializer-enabling-metadata-cache:
370370

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 string $birthday;
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 string $birthday;
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+
.. versionadded:: 6.2
430+
431+
The option to configure a ``SerializedPath`` was introduced in Symfony 6.2.
432+
433+
Using the configuration from above, denormalizing with a metadata-aware
434+
normalizer will write the ``birthday`` field from ``$data`` onto the ``Person``
435+
object::
436+
437+
$data = [
438+
'profile' => [
439+
'information' => [
440+
'birthday' => '01-01-1970',
441+
],
442+
],
443+
];
444+
$person = $normalizer->denormalize($data, Person::class, 'any');
445+
$person->getBirthday(); // 01-01-1970
446+
447+
When using annotations or attributes, the ``SerializedPath`` can either
448+
be set on the property or the associated getter. The ``SerializedPath``
449+
cannot be used in combination with a ``SerializedName`` for the same propety.
450+
The given path must be a string that can be parsed as a ``PropertyPath``.
451+
371452
Configuring the Metadata Cache
372453
------------------------------
373454

0 commit comments

Comments
 (0)