diff --git a/components/serializer.rst b/components/serializer.rst index 5bb34d24a7a..a9a0f7818be 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -922,17 +922,22 @@ The ``setCircularReferenceLimit()`` method of this normalizer sets the number of times it will serialize the same object before considering it a circular reference. Its default value is ``1``. +.. deprecated:: 4.2 + + The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler` + method is deprecated since Symfony 4.2, use the "circular_reference_handler" key of the context instead. + Instead of throwing an exception, circular references can also be handled by custom callables. This is especially useful when serializing entities having unique identifiers:: $encoder = new JsonEncoder(); - $normalizer = new ObjectNormalizer(); - - // all callback parameters are optional (you can omit the ones you don't use) - $normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = []) { - return $object->getName(); - }); + $defaultContext = [ + AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) { + return $object->getName(); + } + ]; + $normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext); $serializer = new Serializer([$normalizer], [$encoder]); var_dump($serializer->serialize($org, 'json'));