Skip to content

Commit 464b578

Browse files
committed
feature #5360 [Serializer] Array Denormalization (derrabus)
This PR was squashed before being merged into the 2.8 branch (closes #5360). Discussion ---------- [Serializer] Array Denormalization | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#14756) | Applies to | 2.8 | Fixed tickets | none This PR adds documentation for the new `ArrayDenormalizer` class to the serializer documentation. Commits ------- 9718c14 [Serializer] Array Denormalization
2 parents dd0e33f + 9718c14 commit 464b578

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

components/serializer.rst

+49
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,55 @@ having unique identifiers::
575575
echo $serializer->serialize($org, 'json');
576576
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
577577

578+
Handling Arrays
579+
---------------
580+
581+
The Serializer component is capable of handling arrays of objects as well.
582+
Serializing arrays works just like serializing a single object::
583+
584+
use Acme\Person;
585+
586+
$person1 = new Person();
587+
$person1->setName('foo');
588+
$person1->setAge(99);
589+
$person1->setSportsman(false);
590+
591+
$person2 = new Person();
592+
$person2->setName('bar');
593+
$person2->setAge(33);
594+
$person2->setSportsman(true);
595+
596+
$persons = array($person1, $person2);
597+
$data = $serializer->serialize($persons, 'json');
598+
599+
// $data contains [{"name":"foo","age":99,"sportsman":false},{"name":"bar","age":33,"sportsman":true}]
600+
601+
.. versionadded:: 2.8
602+
The :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer`
603+
class was introduced in 2.8. Prior to Symfony 2.8, only the serialization of
604+
arrays is supported.
605+
606+
If you want to deserialize such a structure, you need to add the
607+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer`
608+
to the set of normalizers. By appending ``[]`` to the type parameter of the
609+
:method:`Symfony\\Component\\Serializer\\Serializer::deserialize` method,
610+
you indicate that you're expecting an array instead of a single object.
611+
612+
.. code-block:: php
613+
614+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
615+
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
616+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
617+
use Symfony\Component\Serializer\Serializer;
618+
619+
$serializer = new Serializer(
620+
array(new GetSetMethodNormalizer(), new ArrayDenormalizer()),
621+
array(new JsonEncoder())
622+
);
623+
624+
$data = ...; // The serialized data from the previous example
625+
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
626+
578627
.. seealso::
579628

580629
A popular alternative to the Symfony Serializer Component is the third-party

0 commit comments

Comments
 (0)