Skip to content

Commit deedeea

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Serializer] Add documentation about context and Serializer::EMPTY_ARRAY_AS_OBJECT
2 parents 760ba40 + 889cbd6 commit deedeea

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

serializer.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ properties and setters (``setXxx()``) to change properties:
144144
;
145145
};
146146
147+
Serializer Context
148+
------------------
149+
150+
The serializer can define a context to control how the (de)serialization of
151+
resources. This context is passed to all normalizers. For example:
152+
153+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\DateTimeNormalizer` uses
154+
``datetime_format`` key as date time format;
155+
* :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer`
156+
uses ``empty_iterable_as_object`` to represent empty objects as ``{}`` instead
157+
of ``[]`` in JSON.
158+
159+
.. versionadded:: 5.4
160+
161+
The usage of the ``empty_arrays_as_object`` option by default in the
162+
Serializer was introduced in Symfony 5.4.
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+
147174
.. _serializer-using-serialization-groups-annotations:
148175

149176
Using Serialization Groups Annotations

0 commit comments

Comments
 (0)