@@ -655,7 +655,7 @@ When serializing, you can set a callback to format a specific object property::
655
655
$encoder = new JsonEncoder();
656
656
657
657
// all callback parameters are optional (you can omit the ones you don't use)
658
- $callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
658
+ $dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
659
659
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
660
660
};
661
661
@@ -677,6 +677,11 @@ When serializing, you can set a callback to format a specific object property::
677
677
$serializer->serialize($person, 'json');
678
678
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
679
679
680
+ .. deprecated :: 4.2
681
+
682
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCallbacks ` is deprecated since
683
+ Symfony 4.2, use the "callbacks" key of the context instead.
684
+
680
685
.. _component-serializer-normalizers :
681
686
682
687
Normalizers
@@ -947,15 +952,9 @@ when such a case is encountered::
947
952
948
953
echo $serializer->serialize($organization, 'json'); // Throws a CircularReferenceException
949
954
950
- The ``setCircularReferenceLimit() `` method of this normalizer sets the number
951
- of times it will serialize the same object before considering it a circular
952
- reference. Its default value is ``1 ``.
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.
955
+ The key ``circular_reference_limit `` in the default context sets the number of
956
+ times it will serialize the same object before considering it a circular
957
+ reference. The default value is ``1 ``.
959
958
960
959
Instead of throwing an exception, circular references can also be handled
961
960
by custom callables. This is especially useful when serializing entities
@@ -973,6 +972,12 @@ having unique identifiers::
973
972
var_dump($serializer->serialize($org, 'json'));
974
973
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
975
974
975
+ .. deprecated :: 4.2
976
+
977
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
978
+ method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
979
+ key of the context instead.
980
+
976
981
Handling Serialization Depth
977
982
----------------------------
978
983
@@ -1100,11 +1105,16 @@ having unique identifiers::
1100
1105
$level2->child = $level3;
1101
1106
1102
1107
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1103
- $normalizer = new ObjectNormalizer($classMetadataFactory);
1108
+
1104
1109
// all callback parameters are optional (you can omit the ones you don't use)
1105
- $normalizer->setMaxDepthHandler( function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1110
+ $maxDepthHandler = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
1106
1111
return '/foos/'.$innerObject->id;
1107
- });
1112
+ };
1113
+
1114
+ $defaultContext = [
1115
+ AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler,
1116
+ ];
1117
+ $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, $defaultContext);
1108
1118
1109
1119
$serializer = new Serializer([$normalizer]);
1110
1120
@@ -1119,6 +1129,12 @@ having unique identifiers::
1119
1129
];
1120
1130
*/
1121
1131
1132
+ .. deprecated :: 4.2
1133
+
1134
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setMaxDepthHandler `
1135
+ method is deprecated since Symfony 4.2. Use the ``max_depth_handler ``
1136
+ key of the context instead.
1137
+
1122
1138
Handling Arrays
1123
1139
---------------
1124
1140
0 commit comments