Skip to content

Commit e3dff17

Browse files
committed
minor #15580 [Serializer] Add documentation about context and Serializer::EMPTY_ARRAY_AS_OBJECT (lyrixx)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Serializer] Add documentation about context and Serializer::EMPTY_ARRAY_AS_OBJECT fixes #15554 Commits ------- fac03e3 [Serializer] Add documentation about context and Serializer::EMPTY_ARRAY_AS_OBJECT
2 parents e80f3ce + fac03e3 commit e3dff17

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

serializer.rst

+23
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,29 @@ properties and setters (``setXxx()``) to change properties:
148148
;
149149
};
150150
151+
Serializer context
152+
------------------
153+
154+
The serializer can use a (de)serialization context to control how a resource is (de)serialized.
155+
The context is passed to all normalizers. For example:
156+
157+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\DateTimeNormalizer` uses ``datetime_format`` key as date time format;
158+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer` uses ``empty_iterable_as_object`` to preserve empty objects (keeps ``{}`` instead of ``[]`` in JSON);
159+
160+
.. versionadded:: 5.4
161+
162+
:class:`Symfony\\Component\\Serializer\\Serializer` uses ``empty_arrays_as_object`` to serialize empty array as object (uses ``{}`` instead of ``[]`` in JSON);
163+
164+
You can pass the context like following::
165+
166+
$serializer->serialize($something, 'json', [
167+
DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s',
168+
]);
169+
170+
$serializer->deserialize($someJson, Something::class, 'json', [
171+
DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s',
172+
]);
173+
151174
.. _serializer-using-serialization-groups-annotations:
152175

153176
Using Serialization Groups Annotations

0 commit comments

Comments
 (0)