Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 27 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.