File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,33 @@ method on the normalizer definition::
181
181
As a final result, the deserializer uses the ``first_name `` attribute as if
182
182
it were ``firstName `` and uses the ``getFirstName `` and ``setFirstName `` methods.
183
183
184
+ Using Callbacks to Serialize DateTime Objects
185
+ ---------------------------------------------
186
+
187
+ If you have DateTime type fields or need special formatting needs when deserializing
188
+ a particular property from your object you can use the callbacks feature::
189
+
190
+ $encoder = new JsonEncoder();
191
+ $normalizer = new GetSetMethodNormalizer();
192
+
193
+ $callback = function ($dateTime) {
194
+ return $dateTime instanceof \DateTime
195
+ ? $dateTime->format(\DateTime::ISO8601)
196
+ : '';
197
+ }
198
+
199
+ $normalizer->setCallbacks(array('createdAt' => $callback));
200
+
201
+ $serializer = new Serializer(array($normalizer), array($encoder));
202
+
203
+ $person = new Acme\Person();
204
+ $person->setName('cordoval');
205
+ $person->setAge(34);
206
+ $person->setCreatedAt(new \DateTime('now'));
207
+
208
+ $serializer->serialize($person, 'json');
209
+ // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
210
+
184
211
JMSSerializer
185
212
-------------
186
213
You can’t perform that action at this time.
0 commit comments