@@ -172,6 +172,112 @@ configuration:
172172 The ability to configure the ``default_context `` option in the
173173 Serializer was introduced in Symfony 5.4.
174174
175+ You can also specify the context on a per-property basis::
176+
177+ .. configuration-block ::
178+
179+ .. code-block :: php-annotations
180+
181+ namespace App\Model;
182+
183+ use Symfony\Component\Serializer\Annotation\Context;
184+ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
185+
186+ class Person
187+ {
188+ /**
189+ * @Context({ DateTimeNormalizer::FORMAT_KEY = 'Y-m-d' })
190+ */
191+ public $createdAt;
192+
193+ // ...
194+ }
195+
196+ .. code-block :: php-attributes
197+
198+ namespace App\Model;
199+
200+ use Symfony\Component\Serializer\Annotation\Context;
201+ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
202+
203+ class Person
204+ {
205+ #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
206+ public $createdAt;
207+
208+ // ...
209+ }
210+
211+ .. code-block :: yaml
212+
213+ App\Model\Person :
214+ attributes :
215+ createdAt :
216+ context :
217+ datetime_format : ' Y-m-d'
218+
219+ .. code-block :: xml
220+
221+ <?xml version =" 1.0" encoding =" UTF-8" ?>
222+ <serializer xmlns =" http://symfony.com/schema/dic/serializer-mapping"
223+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
224+ xsi : schemaLocation =" http://symfony.com/schema/dic/serializer-mapping
225+ https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
226+ >
227+ <class name =" App\Model\Person" >
228+ <attribute name =" createdAt" >
229+ <context >
230+ <entry name =" datetime_format" >Y-m-d</entry >
231+ </context >
232+ </attribute >
233+ </class >
234+ </serializer >
235+
236+ Use the options to specify context specific to normalization or denormalization::
237+
238+ namespace App\Model;
239+
240+ use Symfony\Component\Serializer\Annotation\Context;
241+ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
242+
243+ class Person
244+ {
245+ #[Context(
246+ normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'],
247+ denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339],
248+ )]
249+ public $createdAt;
250+
251+ // ...
252+ }
253+
254+ You can also restrict the usage of a context to some groups::
255+
256+ namespace App\Model;
257+
258+ use Symfony\Component\Serializer\Annotation\Context;
259+ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
260+
261+ class Person
262+ {
263+ #[Serializer\Groups(['extended'])]
264+ #[Serializer\Context([DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339])]
265+ #[Serializer\Context(
266+ context: [DateTimeNormalizer::FORMAT_KEY => \DateTime::RFC3339_EXTENDED],
267+ groups: ['extended'],
268+ )]
269+ public $createdAt;
270+
271+ // ...
272+ }
273+
274+ The attribute/annotation can be repeated as much as needed on a single property.
275+ Context without group is always applied first. Then context for the matching groups are merged in the provided order.
276+
277+ .. versionadded :: 5.3
278+
279+ The ``Context `` attribute, annotation and the configuration options were introduced in Symfony 5.3.
280+
175281.. _serializer-using-serialization-groups-annotations :
176282
177283Using Serialization Groups Annotations
0 commit comments