Skip to content

Commit 97f48e5

Browse files
committed
[Serializer] Docs for the @MaxDepth annotation
1 parent 36a460d commit 97f48e5

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

components/serializer.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,97 @@ having unique identifiers::
627627
var_dump($serializer->serialize($org, 'json'));
628628
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
629629

630+
Handling Serialization Depth
631+
----------------------------
632+
633+
The Serializer component is able to detect and limit the serialization depth. It is especially useful when
634+
serializing large trees. Assume the following data structure::
635+
636+
namespace Acme;
637+
638+
class MyObj
639+
{
640+
public $foo;
641+
642+
/**
643+
* @var self
644+
*/
645+
public $child;
646+
}
647+
648+
$level1 = new MyObj();
649+
$level1->foo = 'level1';
650+
651+
$level2 = new MyObj();
652+
$level2->foo = 'level2';
653+
$level1->child = $level2;
654+
655+
$level3 = new MyObj();
656+
$level3->foo = 'level3';
657+
$level2->child = $level3;
658+
659+
The serializer can be configured to set a maximum depth for a given property. Here, we set it to 2 for the ``$child``
660+
property:
661+
662+
.. configuration-block::
663+
664+
.. code-block:: php-annotations
665+
666+
use Symfony\Component\Serializer\Annotation\MaxDepth;
667+
668+
namespace Acme;
669+
670+
class MyObj
671+
{
672+
/**
673+
* @MaxDepth(2)
674+
*/
675+
public $foo;
676+
677+
// ...
678+
}
679+
680+
.. code-block:: yaml
681+
682+
Acme\MyObj:
683+
attributes:
684+
foo:
685+
max_depth: 2
686+
687+
.. code-block:: xml
688+
689+
<?xml version="1.0" ?>
690+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
691+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
692+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
693+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
694+
>
695+
<class name="Acme\MyObj">
696+
<attribute name="foo">
697+
<max-depth>2</max-depth>
698+
</attribute>
699+
</serializer>
700+
701+
The metadata loader corresponding to the chosen format must be configured in order to use this feature.
702+
It is done automatically when using the Symfony Standard Edition. When using the standalone component, refer
703+
to :ref:`the groups documentation <component-serializer-attributes-groups>` to learn how to do that.
704+
705+
The check is only done if the `enable_max_depth` key of the serializer context is set to ``true``. In the following
706+
example, the third level is not serialized because it is deeper than the configured maximum depth (2).
707+
708+
$result = $serializer->normalize($level1, null, array('enable_max_depth' => true));
709+
/*
710+
$result = array(
711+
'foo' => 'level1',
712+
'child' => array(
713+
'foo' => 'level2',
714+
'child' => array(
715+
'child' => null,
716+
),
717+
),
718+
);
719+
*/
720+
630721
Handling Arrays
631722
---------------
632723

0 commit comments

Comments
 (0)