@@ -813,6 +813,54 @@ because it is deeper than the configured maximum depth of 2::
813813 );
814814 */
815815
816+ Instead of throwing an exception, a custom callable can be called when the maximum depth is reached. This is especially
817+ useful when serializing entities having unique identifiers::
818+
819+ use Doctrine\Common\Annotations\AnnotationReader;
820+ use Symfony\Component\Serializer\Serializer;
821+ use Symfony\Component\Serializer\Annotation\MaxDepth;
822+ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
823+ use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
824+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
825+
826+ class Foo
827+ {
828+ public $id;
829+ /**
830+ * @MaxDepth(1)
831+ */
832+ public $child;
833+ }
834+
835+ $level1 = new Foo();
836+ $level1->id = 1;
837+
838+ $level2 = new Foo();
839+ $level2->id = 2;
840+ $level1->child = $level2;
841+
842+ $level3 = new Foo();
843+ $level3->id = 3;
844+ $level2->child = $level3;
845+
846+ $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
847+ $normalizer = new ObjectNormalizer($classMetadataFactory);
848+ $normalizer->setMaxDepthHandler(function ($foo) {
849+ return '/foos/'.$foo->id;
850+ });
851+
852+ $serializer = new Serializer(array($normalizer));
853+
854+ $result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
855+ /*
856+ $result = array(
857+ 'id' => 1,
858+ 'child' => array(
859+ 'id' => 2,
860+ 'child' => '/foos/3',
861+ );
862+ */
863+
816864Handling Arrays
817865---------------
818866