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