Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Mention adder method to deserialize an array of objects #17956

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

Closed
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
55 changes: 55 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,61 @@ will be thrown. The type enforcement of the properties can be disabled by settin
the serializer context option ``ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT``
to ``true``.

It is also possible to denormalize an array of object. To do so, ensure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is also possible to denormalize an array of object. To do so, ensure
It is also possible to denormalize an array of objects. To do so, ensure

?

the owner object contains a adder method, so the Serializer is able to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the owner object contains a adder method, so the Serializer is able to
the owner object contains an adder method, so the Serializer is able to

guess the type of object to denormalize in your collection. Let's
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
guess the type of object to denormalize in your collection. Let's
guess the type of the object to denormalize in your collection. Let's

rename ``$inner`` to ``$inners`` and create a adder method::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rename ``$inner`` to ``$inners`` and create a adder method::
rename ``$inner`` to ``$inners`` and create an adder method::


namespace Acme;

use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class ObjectOuter
{
private array $inners;

// ...

public function getInners()
{
return $this->inners;
}

public function setInners(array $inners)
{
$this->inners = $inner;
}

// This is the key method that will allow the Serializer
// to know the type of object of your collection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to know the type of object of your collection
// to know the type of the object of your collection

public function addInner(ObjectInner $inner)
{
$this->inners[] = $inner;
}

// ...
}

$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
// ArrayDenormalizer is also needed for the array denormalization to work
$serializer = new Serializer([new DateTimeNormalizer(), $normalizer, new ArrayDenormalizer()]);

$obj = $serializer->denormalize(
[
'inners' => [
['foo' => 'foo', 'bar' => 'bar']
],
'date' => '1988/01/21',
],
ObjectOuter::class
);

Now, the ``$inners`` property contains an array of ``ObjectInner`` objects.

Serializing Interfaces and Abstract Classes
-------------------------------------------

Expand Down