Skip to content

Commit bce81b3

Browse files
humandbweaverryan
authored andcommitted
Added a few sections more
1 parent ed735fb commit bce81b3

File tree

1 file changed

+89
-9
lines changed

1 file changed

+89
-9
lines changed

components/serializer.rst

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
single: Components; Serializer
44

55
The Serializer Component
6-
====================
6+
========================
77

88
The Serializer Component is meant to be used to turn Objects into a
99
specific format (XML, JSON, Yaml, ...) and the other way around.
@@ -14,12 +14,9 @@ simple schema.
1414
.. image:: /images/components/serializer/serializer_workflow.png
1515

1616
As you can see in the picture above, an array is used as a man in
17-
the middle. This way, Encoders will only deal with turning specific
18-
**formats** into **arrays** and vice versa, and Normalizer will deal with
19-
turning specific **objects** into **arrays** and vice versa.
20-
21-
Also, it's clear from the graph the meaning of the following terms: *encode*,
22-
*decode*, *normalize*, *denormalize*, *serialize* and *deserialize*.
17+
the middle. This way, Normalizers will only deal with turning specific
18+
**formats** into **arrays** and vice versa. The same way, Normalizers
19+
will deal with turning specific **objects** into **arrays** and vice versa.
2320

2421
Installation
2522
------------
@@ -30,5 +27,88 @@ You can install the component in many different ways:
3027
* Install it via PEAR ( `pear.symfony.com/Serializer`);
3128
* Install it via Composer (`symfony/serializer` on Packagist).
3229

33-
Encoders
34-
--------
30+
Usage
31+
-----
32+
33+
Using the Serializer component is really simple. We just need to set up
34+
the :class:`Symfony\\Component\\Serializer\\Serializer` specifying
35+
what Encoders and Normalizer are going to be available::
36+
37+
use Symfony\Component\Serializer\Serializer;
38+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
39+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
40+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
41+
42+
$encoders = array(new XmlEncoder(), new JsonEncoder() );
43+
$normalizers = array(new GetSetMethodNormalizer());
44+
45+
$serializer = new Serializer($normalizers, $encoders);
46+
47+
48+
Serializing an object
49+
~~~~~~~~~~~~~~~~~~~~~
50+
51+
For the sake of this example, let's ssume the following class already
52+
exists in our project::
53+
54+
class Person
55+
{
56+
private $age;
57+
private $name;
58+
59+
// Getters
60+
public function getName()
61+
{
62+
return $this->name;
63+
}
64+
65+
public function getAge()
66+
{
67+
return $this->age;
68+
}
69+
70+
// Setters
71+
public function setName($name)
72+
{
73+
$this->name = $name;
74+
}
75+
76+
public function setAge($age)
77+
{
78+
$this->age = $age;
79+
}
80+
}
81+
82+
Now, if we want to serialize this object into JSON, we only need to
83+
use the Serializer service created before::
84+
85+
$person = new Person();
86+
$person->setName('foo');
87+
$person->setAge(99);
88+
89+
$serializer->serialize($person, 'json'); // Output: {"name":"foo","age":99}
90+
91+
The first paramater of the `Serializer::serialize` is the object to be
92+
serialized, the second one will be used to pick the proper encoder,
93+
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.
94+
95+
Deserializing an object
96+
~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
Let's see now how to do the exactly the opposite. This time, the information
99+
of the `People` class would be encoded in XML format::
100+
101+
$data = <<<EOF
102+
<person>
103+
<name>foo</name>
104+
<age>99</age>
105+
</person>
106+
EOF;
107+
108+
$person = $serializer->deserialize($data,'Person','xml');
109+
110+
In this case, `Serializer::deserialize` needs three parameters:
111+
112+
1. The information to be decoded
113+
2. The name of the class this information will be decoded to
114+
3. The encoder used to convert that information into an array

0 commit comments

Comments
 (0)