@@ -639,7 +639,7 @@ When serializing, you can set a callback to format a specific object property::
639
639
$encoder = new JsonEncoder();
640
640
641
641
// all callback parameters are optional (you can omit the ones you don't use)
642
- $callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
642
+ $dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
643
643
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
644
644
};
645
645
@@ -661,6 +661,11 @@ When serializing, you can set a callback to format a specific object property::
661
661
$serializer->serialize($person, 'json');
662
662
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
663
663
664
+ .. deprecated :: 4.2
665
+
666
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCallbacks ` is deprecated since
667
+ Symfony 4.2, use the "callbacks" key of the context instead.
668
+
664
669
.. _component-serializer-normalizers :
665
670
666
671
Normalizers
@@ -931,15 +936,9 @@ when such a case is encountered::
931
936
932
937
echo $serializer->serialize($organization, 'json'); // Throws a CircularReferenceException
933
938
934
- The ``setCircularReferenceLimit() `` method of this normalizer sets the number
935
- of times it will serialize the same object before considering it a circular
936
- reference. Its default value is ``1 ``.
937
-
938
- .. deprecated :: 4.2
939
-
940
- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
941
- method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
942
- key of the context instead.
939
+ The key ``circular_reference_limit `` in the default context sets the number of
940
+ times it will serialize the same object before considering it a circular
941
+ reference. The default value is ``1 ``.
943
942
944
943
Instead of throwing an exception, circular references can also be handled
945
944
by custom callables. This is especially useful when serializing entities
@@ -957,6 +956,12 @@ having unique identifiers::
957
956
var_dump($serializer->serialize($org, 'json'));
958
957
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
959
958
959
+ .. deprecated :: 4.2
960
+
961
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
962
+ method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
963
+ key of the context instead.
964
+
960
965
Handling Serialization Depth
961
966
----------------------------
962
967
@@ -1084,11 +1089,16 @@ having unique identifiers::
1084
1089
$level2->child = $level3;
1085
1090
1086
1091
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1087
- $normalizer = new ObjectNormalizer($classMetadataFactory);
1092
+
1088
1093
// all callback parameters are optional (you can omit the ones you don't use)
1089
- $normalizer->setMaxDepthHandler( function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1094
+ $maxDepthHandler = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1090
1095
return '/foos/'.$innerObject->id;
1091
- });
1096
+ };
1097
+
1098
+ $defaultContext = [
1099
+ AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler,
1100
+ ];
1101
+ $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, $defaultContext);
1092
1102
1093
1103
$serializer = new Serializer([$normalizer]);
1094
1104
@@ -1103,6 +1113,12 @@ having unique identifiers::
1103
1113
];
1104
1114
*/
1105
1115
1116
+ .. deprecated :: 4.2
1117
+
1118
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setMaxDepthHandler `
1119
+ method is deprecated since Symfony 4.2. Use the ``max_depth_handler ``
1120
+ key of the context instead.
1121
+
1106
1122
Handling Arrays
1107
1123
---------------
1108
1124
0 commit comments