Skip to content

[Serializer] Allow to access to the context and various other infos in callbacks and max depth handler #10318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,9 @@ When serializing, you can set a callback to format a specific object property::
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();

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

$normalizer->setCallbacks(array('createdAt' => $callback));
Expand All @@ -563,6 +562,10 @@ When serializing, you can set a callback to format a specific object property::
$serializer->serialize($person, 'json');
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}

.. versionadded:: 4.2
The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context``
parameters of the callback were introduced in Symfony 4.2.

.. _component-serializer-normalizers:

Normalizers
Expand Down Expand Up @@ -828,14 +831,19 @@ having unique identifiers::
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();

$normalizer->setCircularReferenceHandler(function ($object) {
// all callback parameters are optional (you can omit the ones you don't use)
$normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = array()) {
return $object->getName();
});

$serializer = new Serializer(array($normalizer), array($encoder));
var_dump($serializer->serialize($org, 'json'));
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}

.. versionadded:: 4.2
The ``$format`` and ``$context`` parameters of ``setCircularReferenceHandler()``
were introduced in Symfony 4.2.

Handling Serialization Depth
----------------------------

Expand Down Expand Up @@ -964,8 +972,9 @@ having unique identifiers::

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$normalizer->setMaxDepthHandler(function ($foo) {
return '/foos/'.$foo->id;
// all callback parameters are optional (you can omit the ones you don't use)
$normalizer->setMaxDepthHandler(function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = array()) {
return '/foos/'.$innerObject->id;
});

$serializer = new Serializer(array($normalizer));
Expand All @@ -984,6 +993,10 @@ having unique identifiers::
.. versionadded:: 4.1
The ``setMaxDepthHandler()`` method was introduced in Symfony 4.1.

.. versionadded:: 4.2
The ``$outerObject``, ``$attributeName``, ``$format`` and ``$context``
parameters of ``setMaxDepthHandler()`` were introduced in Symfony 4.2.

Handling Arrays
---------------

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy