Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec06cc7

Browse files
committed
minor #11384 Update serializer.rst (Raulnet)
This PR was merged into the 4.2 branch. Discussion ---------- Update serializer.rst shows the new way to define a `circular_reference_handler` on the normalizer symfony/symfony#30998 Commits ------- d562333 Update serializer.rst
2 parents 7e27e51 + d562333 commit ec06cc7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

components/serializer.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,17 +922,22 @@ The ``setCircularReferenceLimit()`` method of this normalizer sets the number
922922
of times it will serialize the same object before considering it a circular
923923
reference. Its default value is ``1``.
924924

925+
.. deprecated:: 4.2
926+
927+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler`
928+
method is deprecated since Symfony 4.2, use the "circular_reference_handler" key of the context instead.
929+
925930
Instead of throwing an exception, circular references can also be handled
926931
by custom callables. This is especially useful when serializing entities
927932
having unique identifiers::
928933

929934
$encoder = new JsonEncoder();
930-
$normalizer = new ObjectNormalizer();
931-
932-
// all callback parameters are optional (you can omit the ones you don't use)
933-
$normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = []) {
934-
return $object->getName();
935-
});
935+
$defaultContext = [
936+
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
937+
return $object->getName();
938+
}
939+
];
940+
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
936941

937942
$serializer = new Serializer([$normalizer], [$encoder]);
938943
var_dump($serializer->serialize($org, 'json'));

0 commit comments

Comments
 (0)