Skip to content

Commit 695b706

Browse files
authored
Update serializer.rst: deprecate the setCallbacks
Change the deprecated setCallbacks method to the callbacks context option
1 parent 327170e commit 695b706

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

components/serializer.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ and ``remove``.
623623
Using Callbacks to Serialize Properties with Object Instances
624624
-------------------------------------------------------------
625625

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+
626632
When serializing, you can set a callback to format a specific object property::
627633

628634
use App\Model\Person;
@@ -631,14 +637,19 @@ When serializing, you can set a callback to format a specific object property::
631637
use Symfony\Component\Serializer\Serializer;
632638

633639
$encoder = new JsonEncoder();
634-
$normalizer = new GetSetMethodNormalizer();
635640

636641
// all callback parameters are optional (you can omit the ones you don't use)
637642
$callback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
638643
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
639644
};
640645

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);
642653

643654
$serializer = new Serializer([$normalizer], [$encoder]);
644655

0 commit comments

Comments
 (0)