@@ -623,6 +623,12 @@ and ``remove``.
623
623
Using Callbacks to Serialize Properties with Object Instances
624
624
-------------------------------------------------------------
625
625
626
+ .. deprecated :: 4.2
627
+
628
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCallbacks `
629
+ method is deprecated since Symfony 4.2. Use the ``callbacks ``
630
+ key of the context instead.
631
+
626
632
When serializing, you can set a callback to format a specific object property::
627
633
628
634
use App\Model\Person;
@@ -631,14 +637,19 @@ When serializing, you can set a callback to format a specific object property::
631
637
use Symfony\Component\Serializer\Serializer;
632
638
633
639
$encoder = new JsonEncoder();
634
- $normalizer = new GetSetMethodNormalizer();
635
640
636
641
// all callback parameters are optional (you can omit the ones you don't use)
637
642
$callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
638
643
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
639
644
};
640
645
641
- $normalizer->setCallbacks(['createdAt' => $callback]);
646
+ $defaultContext = [
647
+ AbstractNormalizer::CALLBACKS => [
648
+ 'createdAt' => $callback,
649
+ ],
650
+ ];
651
+
652
+ $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
642
653
643
654
$serializer = new Serializer([$normalizer], [$encoder]);
644
655
0 commit comments