-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Component][Serializer] Adding a brand new chapter on the Serializer Component #1684
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
.. index:: | ||
single: Serializer | ||
single: Components; Serializer | ||
|
||
The Serializer Component | ||
======================== | ||
|
||
The Serializer Component is meant to be used to turn Objects into a | ||
specific format (XML, JSON, Yaml, ...) and the other way around. | ||
|
||
In order to do so, the Serializer Components follows the following | ||
simple schema. | ||
|
||
.. image:: /images/components/serializer/serializer_workflow.png | ||
|
||
As you can see in the picture above, an array is used as a man in | ||
the middle. This way, Normalizers will only deal with turning specific | ||
**formats** into **arrays** and vice versa. The same way, Normalizers | ||
will deal with turning specific **objects** into **arrays** and vice versa. | ||
|
||
Installation | ||
------------ | ||
|
||
You can install the component in many different ways: | ||
|
||
* Use the official Git repository (https://github.com/symfony/Serializer); | ||
* Install it via PEAR ( `pear.symfony.com/Serializer`); | ||
* Install it via Composer (`symfony/serializer` on Packagist). | ||
|
||
Usage | ||
----- | ||
|
||
Using the Serializer component is really simple. We just need to set up | ||
the :class:`Symfony\\Component\\Serializer\\Serializer` specifying | ||
what Encoders and Normalizer are going to be available:: | ||
|
||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; | ||
|
||
$encoders = array(new XmlEncoder(), new JsonEncoder() ); | ||
$normalizers = array(new GetSetMethodNormalizer()); | ||
|
||
$serializer = new Serializer($normalizers, $encoders); | ||
|
||
|
||
Serializing an object | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
For the sake of this example, let's ssume the following class already | ||
exists in our project:: | ||
|
||
namespace Acme; | ||
|
||
class Person | ||
{ | ||
private $age; | ||
private $name; | ||
|
||
// Getters | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getAge() | ||
{ | ||
return $this->age; | ||
} | ||
|
||
// Setters | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function setAge($age) | ||
{ | ||
$this->age = $age; | ||
} | ||
} | ||
|
||
Now, if we want to serialize this object into JSON, we only need to | ||
use the Serializer service created before:: | ||
|
||
$person = new Acme\Person(); | ||
$person->setName('foo'); | ||
$person->setAge(99); | ||
|
||
$serializer->serialize($person, 'json'); // Output: {"name":"foo","age":99} | ||
|
||
The first paramater of the `Serializer::serialize` is the object to be | ||
serialized, the second one will be used to pick the proper encoder, | ||
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`. | ||
|
||
Deserializing an object | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Let's see now how to do the exactly the opposite. This time, the information | ||
of the `People` class would be encoded in XML format:: | ||
|
||
$data = <<<EOF | ||
<person> | ||
<name>foo</name> | ||
<age>99</age> | ||
</person> | ||
EOF; | ||
|
||
$person = $serializer->deserialize($data,'Acme\Person','xml'); | ||
|
||
In this case, `Serializer::deserialize` needs three parameters: | ||
|
||
1. The information to be decoded | ||
2. The name of the class this information will be decoded to | ||
3. The encoder used to convert that information into an array |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You should remove the space between
JsonEncoder()
and);
(and maybe put this on 3 lines with a comma after each item)