Skip to content

[Serializer] Add documentation for new feature in ArrayDenormalizer of Symfony Serializer #16489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 6.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,28 @@ you indicate that you're expecting an array instead of a single object::
$data = ...; // The serialized data from the previous example
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');


By default the :class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer`
will give the whole array to other denormalizers when checking for support.
Sometimes a denormalizer will also check if the data has the correct datatype,
and may fail because it expected the datatype of an element inside the array.

In such cases it's possible to pass ``true`` for the first argument of
:class:`Symfony\\Component\\Serializer\\Normalizer\\ArrayDenormalizer`
to indicate that only the first element of the array must be checked::

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(
[new GetSetMethodNormalizer(), new ArrayDenormalizer(true)],
[new JsonEncoder()]
);

$data = ...; // The serialized data from the previous example
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
Handling Constructor Arguments
------------------------------

Expand Down