-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
the owner object contains a adder method, so the Serializer is able to | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
rename ``$inner`` to ``$inners`` and create a adder method:: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
------------------------------------------- | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?