Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
javiereguiluz merged 3 commits intosymfony:masterfromdunglas:pr_27017
Sep 11, 2018
Merged
Changes fromall 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 deletionscomponents/serializer.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp