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