-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
[Serializer] Array Denormalization #5360
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 |
---|---|---|
|
@@ -573,6 +573,55 @@ 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 arrays works just like serializing a single object:: | ||
|
||
use Acme\Person; | ||
|
||
$person1 = new Person(); | ||
$person1->setName('foo'); | ||
$person1->setAge(99); | ||
$person1->setSportsman(false); | ||
|
||
$person2 = new Person(); | ||
$person2->setName('bar'); | ||
$person2->setAge(33); | ||
$person2->setSportsman(true); | ||
|
||
$persons = array($person1, $person2); | ||
$data = $serializer->serialize($persons, 'json'); | ||
|
||
// $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. | ||
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. This should be moved before the previous paragraph. |
||
|
||
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; | ||
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()) | ||
); | ||
|
||
$data = ...; // The serialized data from the previous example | ||
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json'); | ||
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. We should have something like 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. You're right. |
||
|
||
.. seealso:: | ||
|
||
A popular alternative to the Symfony Serializer Component is the third-party | ||
|
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.
I would love to make this code sample a bit less foobar-ish and not advertise the use of setters:
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.
I agree. However, I adopted this style from the paragraph Serializing an Object of the same document. If we change this, we should change the other examples as well, shouldn't we?