@@ -629,14 +629,15 @@ When serializing, you can set a callback to format a specific object property::
629629 use Symfony\Component\Serializer\Serializer;
630630
631631 $encoder = new JsonEncoder();
632- $normalizer = new GetSetMethodNormalizer();
633632
634633 // all callback parameters are optional (you can omit the ones you don't use)
635- $callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
634+ $dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
636635 return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
637636 };
638637
639- $normalizer->setCallbacks(['createdAt' => $callback]);
638+ $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, null, $defaultContext = [
639+ AbstractNormalizer::CALLBACKS => ['createdAt' => $dateCallback]
640+ ]);
640641
641642 $serializer = new Serializer([$normalizer], [$encoder]);
642643
@@ -648,6 +649,11 @@ When serializing, you can set a callback to format a specific object property::
648649 $serializer->serialize($person, 'json');
649650 // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
650651
652+ .. deprecated :: 4.2
653+
654+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCallbacks ` is deprecated since
655+ Symfony 4.2, use the "callbacks" key of the context instead.
656+
651657.. _component-serializer-normalizers :
652658
653659Normalizers
@@ -918,16 +924,16 @@ when such a case is encountered::
918924
919925 echo $serializer->serialize($organization, 'json'); // Throws a CircularReferenceException
920926
921- The ``setCircularReferenceLimit() `` method of this normalizer sets the number
922- of times it will serialize the same object before considering it a circular
923- reference. Its default value is ``1 ``.
924-
925927.. deprecated :: 4.2
926928
927- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
928- method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
929+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceLimit `
930+ method is deprecated since Symfony 4.2. Use the ``circular_reference_limit ``
929931 key of the context instead.
930932
933+ The key ``circular_reference_limit `` in the defaultContext, sets the number of times it will serialize the
934+ same object before considering it a circular reference.
935+ In the ``$defaultContext `` the default value is ``1 ``.
936+
931937Instead of throwing an exception, circular references can also be handled
932938by custom callables. This is especially useful when serializing entities
933939having unique identifiers::
@@ -944,6 +950,12 @@ having unique identifiers::
944950 var_dump($serializer->serialize($org, 'json'));
945951 // {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
946952
953+ .. deprecated :: 4.2
954+
955+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
956+ method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
957+ key of the context instead.
958+
947959Handling Serialization Depth
948960----------------------------
949961
@@ -1071,11 +1083,15 @@ having unique identifiers::
10711083 $level2->child = $level3;
10721084
10731085 $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1074- $normalizer = new ObjectNormalizer($classMetadataFactory);
1086+
10751087 // all callback parameters are optional (you can omit the ones you don't use)
1076- $normalizer->setMaxDepthHandler( function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1088+ $maxDepthHandler = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
10771089 return '/foos/'.$innerObject->id;
1078- });
1090+ };
1091+
1092+ $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, [
1093+ AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler
1094+ ]);
10791095
10801096 $serializer = new Serializer([$normalizer]);
10811097
@@ -1090,6 +1106,12 @@ having unique identifiers::
10901106 ];
10911107 */
10921108
1109+ .. deprecated :: 4.2
1110+
1111+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setMaxDepthHandler `
1112+ method is deprecated since Symfony 4.2. Use the ``max_depth_handler ``
1113+ key of the context instead
1114+
10931115Handling Arrays
10941116---------------
10951117
0 commit comments