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