@@ -629,14 +629,15 @@ When serializing, you can set a callback to format a specific object property::
629
629
use Symfony\Component\Serializer\Serializer;
630
630
631
631
$encoder = new JsonEncoder();
632
- $normalizer = new GetSetMethodNormalizer();
633
632
634
633
// 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 = []) {
636
635
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
637
636
};
638
637
639
- $normalizer->setCallbacks(['createdAt' => $callback]);
638
+ $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, null, $defaultContext = [
639
+ AbstractNormalizer::CALLBACKS => ['createdAt' => $dateCallback]
640
+ ]);
640
641
641
642
$serializer = new Serializer([$normalizer], [$encoder]);
642
643
@@ -648,6 +649,11 @@ When serializing, you can set a callback to format a specific object property::
648
649
$serializer->serialize($person, 'json');
649
650
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
650
651
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
+
651
657
.. _component-serializer-normalizers :
652
658
653
659
Normalizers
@@ -918,16 +924,16 @@ when such a case is encountered::
918
924
919
925
echo $serializer->serialize($organization, 'json'); // Throws a CircularReferenceException
920
926
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
-
925
927
.. deprecated :: 4.2
926
928
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 ``
929
931
key of the context instead.
930
932
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
+
931
937
Instead of throwing an exception, circular references can also be handled
932
938
by custom callables. This is especially useful when serializing entities
933
939
having unique identifiers::
@@ -944,6 +950,12 @@ having unique identifiers::
944
950
var_dump($serializer->serialize($org, 'json'));
945
951
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
946
952
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
+
947
959
Handling Serialization Depth
948
960
----------------------------
949
961
@@ -1071,11 +1083,15 @@ having unique identifiers::
1071
1083
$level2->child = $level3;
1072
1084
1073
1085
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
1074
- $normalizer = new ObjectNormalizer($classMetadataFactory);
1086
+
1075
1087
// 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 = []) {
1077
1089
return '/foos/'.$innerObject->id;
1078
- });
1090
+ };
1091
+
1092
+ $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, [
1093
+ AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler
1094
+ ]);
1079
1095
1080
1096
$serializer = new Serializer([$normalizer]);
1081
1097
@@ -1090,6 +1106,12 @@ having unique identifiers::
1090
1106
];
1091
1107
*/
1092
1108
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
+
1093
1115
Handling Arrays
1094
1116
---------------
1095
1117
0 commit comments