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