5
5
The Serializer Component
6
6
========================
7
7
8
- The Serializer Component is meant to be used to turn Objects into a
8
+ The Serializer Component is meant to be used to turn objects into a
9
9
specific format (XML, JSON, Yaml, ...) and the other way around.
10
10
11
- In order to do so, the Serializer Components follows the following
11
+ In order to do so, the Serializer Component follows the following
12
12
simple schema.
13
13
14
14
.. image :: /images/components/serializer/serializer_workflow.png
15
15
16
16
As you can see in the picture above, an array is used as a man in
17
- the middle. This way, Normalizers will only deal with turning specific
17
+ the middle. This way, Serializers will only deal with turning specific
18
18
**formats ** into **arrays ** and vice versa. The same way, Normalizers
19
19
will deal with turning specific **objects ** into **arrays ** and vice versa.
20
20
21
+ Serialization is a complicated topic, and while this component may not work
22
+ in all cases, it can be a useful tool while developing tools to serialize
23
+ and deserialize your objects.
24
+
21
25
Installation
22
26
------------
23
27
@@ -31,24 +35,23 @@ Usage
31
35
-----
32
36
33
37
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::
38
+ the :class: `Symfony\\ Component\\ Serializer\\ Serializer ` specifying
39
+ which Encoders and Normalizer are going to be available::
36
40
37
41
use Symfony\Component\Serializer\Serializer;
38
42
use Symfony\Component\Serializer\Encoder\XmlEncoder;
39
43
use Symfony\Component\Serializer\Encoder\JsonEncoder;
40
44
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
41
45
42
- $encoders = array(new XmlEncoder(), new JsonEncoder() );
46
+ $encoders = array(new XmlEncoder(), new JsonEncoder());
43
47
$normalizers = array(new GetSetMethodNormalizer());
44
48
45
49
$serializer = new Serializer($normalizers, $encoders);
46
50
47
-
48
51
Serializing an object
49
52
~~~~~~~~~~~~~~~~~~~~~
50
53
51
- For the sake of this example, let's ssume the following class already
54
+ For the sake of this example, let's assume the following class already
52
55
exists in our project::
53
56
54
57
namespace Acme;
@@ -90,12 +93,12 @@ use the Serializer service created before::
90
93
91
94
$serializer->serialize($person, 'json'); // Output: {"name":"foo","age":99}
92
95
93
- The first paramater of the ` Serializer::serialize ` is the object to be
94
- serialized, the second one will be used to pick the proper encoder,
96
+ The first parameter of the :method: ` Symfony \\ Component \\ Serializer\\ Serializer ::serialize `
97
+ is the object to be serialized and the second is used to choose the proper encoder,
95
98
in this case :class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `.
96
99
97
- Deserializing an object
98
- ~~~~~~~~~~~~~~~~~~~~~~~~
100
+ Deserializing an Object
101
+ ~~~~~~~~~~~~~~~~~~~~~~~
99
102
100
103
Let's see now how to do the exactly the opposite. This time, the information
101
104
of the `People ` class would be encoded in XML format::
@@ -109,8 +112,20 @@ of the `People` class would be encoded in XML format::
109
112
110
113
$person = $serializer->deserialize($data,'Acme\Person','xml');
111
114
112
- In this case, `Serializer::deserialize ` needs three parameters:
115
+ In this case, :method: `Symfony\\ Component\\ Serializer\\ Serializer::deserialize `
116
+ needs three parameters:
113
117
114
118
1. The information to be decoded
115
119
2. The name of the class this information will be decoded to
116
120
3. The encoder used to convert that information into an array
121
+
122
+ JMSSerializationBundle
123
+ ----------------------
124
+
125
+ A popular third-party bundle, `JMSSerializationBundle `_ exists and extends
126
+ (and sometimes replaces) the serialization functionality. This includes the
127
+ ability to configure how your objects should be serialize/deserialized via
128
+ annotations (as well as YML, XML and PHP), integration with the Doctrine ORM,
129
+ and handling of other complex cases (e.g. circular references).
130
+
131
+ .. _`JMSSerializationBundle` : https://github.com/schmittjoh/JMSSerializerBundle
0 commit comments