From eabdbd01605c9cbe9dc894e7c001a4fe0ed0036b Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Sat, 22 Mar 2014 07:44:56 -0700 Subject: [PATCH 1/3] add serializer set callback documentation --- components/serializer.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index e45dc61f74c..c5a192b360c 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -181,6 +181,33 @@ method on the normalizer definition:: As a final result, the deserializer uses the ``first_name`` attribute as if it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods. +Using Callbacks to Serialize DateTime Objects +--------------------------------------------- + +If you have DateTime type fields or need special formatting needs when deserializing +a particular property from your object you can use the callbacks feature:: + + $encoder = new JsonEncoder(); + $normalizer = new GetSetMethodNormalizer(); + + $callback = function ($dateTime) { + return $dateTime instanceof \DateTime + ? $dateTime->format(\DateTime::ISO8601) + : ''; + } + + $normalizer->setCallbacks(array('createdAt' => $callback)); + + $serializer = new Serializer(array($normalizer), array($encoder)); + + $person = new Acme\Person(); + $person->setName('cordoval'); + $person->setAge(34); + $person->setCreatedAt(new \DateTime('now')); + + $serializer->serialize($person, 'json'); + // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"} + JMSSerializer ------------- From efe2029eccf1aeac7b3913696b666a456da814b4 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Sat, 22 Mar 2014 10:53:06 -0700 Subject: [PATCH 2/3] plug new revision --- components/serializer.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index c5a192b360c..acf6163f551 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -181,11 +181,15 @@ method on the normalizer definition:: As a final result, the deserializer uses the ``first_name`` attribute as if it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods. -Using Callbacks to Serialize DateTime Objects ---------------------------------------------- +Using Callbacks to Serialize Properties With Object Instances +------------------------------------------------------------- -If you have DateTime type fields or need special formatting needs when deserializing -a particular property from your object you can use the callbacks feature:: +When serializing you can set a callback to format a specific object property. + + use Symfony\Component\Serializer\Encoder\JsonEncoder; + use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; + use Symfony\Component\Serializer\Serializer; + use Acme\Person; $encoder = new JsonEncoder(); $normalizer = new GetSetMethodNormalizer(); @@ -200,7 +204,7 @@ a particular property from your object you can use the callbacks feature:: $serializer = new Serializer(array($normalizer), array($encoder)); - $person = new Acme\Person(); + $person = new Person(); $person->setName('cordoval'); $person->setAge(34); $person->setCreatedAt(new \DateTime('now')); From b865b4049218c69be879ebed9c490a562b862ca8 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Mon, 24 Mar 2014 08:26:48 -0500 Subject: [PATCH 3/3] add comma madness and lowercasing W --- components/serializer.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index acf6163f551..f84718286b6 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -181,15 +181,15 @@ method on the normalizer definition:: As a final result, the deserializer uses the ``first_name`` attribute as if it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods. -Using Callbacks to Serialize Properties With Object Instances +Using Callbacks to Serialize Properties with Object Instances ------------------------------------------------------------- -When serializing you can set a callback to format a specific object property. +When serializing, you can set a callback to format a specific object property:: + use Acme\Person; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; use Symfony\Component\Serializer\Serializer; - use Acme\Person; $encoder = new JsonEncoder(); $normalizer = new GetSetMethodNormalizer();