From 6290e6a58a4438f56ba3f74aca4ae3a72aad8551 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 4 Jun 2015 01:30:28 +0200 Subject: [PATCH 1/3] Added documentation about array denormalization. --- components/serializer.rst | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index eca1925cb0a..6dd1d84aa3b 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -573,6 +573,52 @@ having unique identifiers:: echo $serializer->serialize($org, 'json'); // {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]} +Handling Arrays +--------------- + +The serializer component is capable of handling arrays of objects as well. +Serializing such an array works just like serializing a single object: + +.. code-block:: php + + $person1 = new Acme\Person(); + $person1->setName('foo'); + $person1->setAge(99); + $person1->setSportsman(false); + + $person2 = new Acme\Person(); + $person2->setName('bar'); + $person2->setAge(33); + $person2->setSportsman(true); + + $jsonContent = $serializer->serialize(array($person1, $person2), 'json'); + + // $jsonContent contains [{"name":"foo","age":99,"sportsman":false},{"name":"bar","age":33,"sportsman":true}] + +If you want to deserialize such a structure, you need to add the :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` +to the set of normalizers. By appending ``[]`` to the type parameter of the +:method:`Symfony\\Component\\Serializer\\Serializer::deserialize` method, +you indicate that you're expecting an array instead of a single object. + +.. versionadded:: 2.8 + The :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` + class was added in 2.8. In previous versions, only the serialization of + arrays is supported. + +.. code-block:: php + + use Symfony\Component\Serializer\Encoder\JsonEncoder; + use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; + use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; + use Symfony\Component\Serializer\Serializer; + + $serializer = new Serializer( + array(new GetSetMethodNormalizer, new ArrayDenormalizer), + array(new JsonEncoder) + ); + + $persons = $serializer->deserialize($data, 'Acme\Person[]', 'json'); + .. seealso:: A popular alternative to the Symfony Serializer Component is the third-party From d7f00ac8679573351b5620ec2af636c6db4a9542 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 9 Jun 2015 20:09:52 +0200 Subject: [PATCH 2/3] Minor changes, as suggested by @WouterJ. --- components/serializer.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 6dd1d84aa3b..9eefe338356 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -576,17 +576,17 @@ having unique identifiers:: Handling Arrays --------------- -The serializer component is capable of handling arrays of objects as well. -Serializing such an array works just like serializing a single object: +The Serializer component is capable of handling arrays of objects as well. +Serializing arrays works just like serializing a single object:: -.. code-block:: php + use Acme\Person; - $person1 = new Acme\Person(); + $person1 = new Person(); $person1->setName('foo'); $person1->setAge(99); $person1->setSportsman(false); - $person2 = new Acme\Person(); + $person2 = new Person(); $person2->setName('bar'); $person2->setAge(33); $person2->setSportsman(true); @@ -602,7 +602,7 @@ you indicate that you're expecting an array instead of a single object. .. versionadded:: 2.8 The :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` - class was added in 2.8. In previous versions, only the serialization of + class was introduced in 2.8. Prior to Symfony 2.8, only the serialization of arrays is supported. .. code-block:: php @@ -613,8 +613,8 @@ you indicate that you're expecting an array instead of a single object. use Symfony\Component\Serializer\Serializer; $serializer = new Serializer( - array(new GetSetMethodNormalizer, new ArrayDenormalizer), - array(new JsonEncoder) + array(new GetSetMethodNormalizer(), new ArrayDenormalizer()), + array(new JsonEncoder()) ); $persons = $serializer->deserialize($data, 'Acme\Person[]', 'json'); From 723f8df963a330c66eb989613bcd95d05937cea4 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 11 Jun 2015 12:27:43 +0100 Subject: [PATCH 3/3] Minor changes as suggested by @xabbuh. --- components/serializer.rst | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 9eefe338356..a6887fb3132 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -591,20 +591,22 @@ Serializing arrays works just like serializing a single object:: $person2->setAge(33); $person2->setSportsman(true); - $jsonContent = $serializer->serialize(array($person1, $person2), 'json'); + $persons = array($person1, $person2); + $data = $serializer->serialize($persons, 'json'); - // $jsonContent contains [{"name":"foo","age":99,"sportsman":false},{"name":"bar","age":33,"sportsman":true}] - -If you want to deserialize such a structure, you need to add the :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` -to the set of normalizers. By appending ``[]`` to the type parameter of the -:method:`Symfony\\Component\\Serializer\\Serializer::deserialize` method, -you indicate that you're expecting an array instead of a single object. + // $data contains [{"name":"foo","age":99,"sportsman":false},{"name":"bar","age":33,"sportsman":true}] .. versionadded:: 2.8 The :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` class was introduced in 2.8. Prior to Symfony 2.8, only the serialization of arrays is supported. +If you want to deserialize such a structure, you need to add the +:class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer` +to the set of normalizers. By appending ``[]`` to the type parameter of the +:method:`Symfony\\Component\\Serializer\\Serializer::deserialize` method, +you indicate that you're expecting an array instead of a single object. + .. code-block:: php use Symfony\Component\Serializer\Encoder\JsonEncoder; @@ -617,6 +619,7 @@ you indicate that you're expecting an array instead of a single object. array(new JsonEncoder()) ); + $data = ...; // The serialized data from the previous example $persons = $serializer->deserialize($data, 'Acme\Person[]', 'json'); .. seealso::