@@ -47,11 +47,20 @@ which Encoders and Normalizer are going to be available::
47
47
use Symfony\Component\Serializer\Encoder\JsonEncoder;
48
48
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
49
49
50
+ // Allows to serialize/deserialize in JSON and XML
50
51
$encoders = array(new XmlEncoder(), new JsonEncoder());
52
+ // Allows to normalize objects thanks to their getters and setters
51
53
$normalizers = array(new GetSetMethodNormalizer());
52
54
53
55
$serializer = new Serializer($normalizers, $encoders);
54
56
57
+ The following examples assume that you instantiate the serializer as above.
58
+
59
+ .. note ::
60
+
61
+ Read the dedicated sections to learn more about :doc: `/components/serializer/encoders `
62
+ and :doc: `/components/serializer/normalizers `.
63
+
55
64
Serializing an Object
56
65
---------------------
57
66
@@ -65,7 +74,7 @@ exists in your project::
65
74
private $age;
66
75
private $name;
67
76
68
- // Getters
77
+ // Getter
69
78
public function getName()
70
79
{
71
80
return $this->name;
@@ -105,29 +114,6 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
105
114
is the object to be serialized and the second is used to choose the proper encoder,
106
115
in this case :class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `.
107
116
108
- Ignoring Attributes when Serializing
109
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
-
111
- .. versionadded :: 2.3
112
- The :method: `GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes> `
113
- method was introduced in Symfony 2.3.
114
-
115
- As an option, there's a way to ignore attributes from the origin object when
116
- serializing. To remove those attributes use the
117
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
118
- method on the normalizer definition::
119
-
120
- use Symfony\Component\Serializer\Serializer;
121
- use Symfony\Component\Serializer\Encoder\JsonEncoder;
122
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
123
-
124
- $normalizer = new GetSetMethodNormalizer();
125
- $normalizer->setIgnoredAttributes(array('age'));
126
- $encoder = new JsonEncoder();
127
-
128
- $serializer = new Serializer(array($normalizer), array($encoder));
129
- $serializer->serialize($person, 'json'); // Output: {"name":"foo"}
130
-
131
117
Deserializing an Object
132
118
-----------------------
133
119
@@ -141,6 +127,7 @@ of the ``Person`` class would be encoded in XML format::
141
127
</person>
142
128
EOF;
143
129
130
+ // Will return an instance of Acme\Person
144
131
$person = $serializer->deserialize($data, 'Acme\Person', 'xml');
145
132
146
133
In this case, :method: `Symfony\\ Component\\ Serializer\\ Serializer::deserialize `
@@ -150,69 +137,11 @@ needs three parameters:
150
137
#. The name of the class this information will be decoded to
151
138
#. The encoder used to convert that information into an array
152
139
153
- Using Camelized Method Names for Underscored Attributes
154
- -------------------------------------------------------
155
-
156
- .. versionadded :: 2.3
157
- The :method: `GetSetMethodNormalizer::setCamelizedAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setCamelizedAttributes> `
158
- method was introduced in Symfony 2.3.
159
-
160
- Sometimes property names from the serialized content are underscored (e.g.
161
- ``first_name ``). Normally, these attributes will use get/set methods like
162
- ``getFirst_name ``, when ``getFirstName `` method is what you really want. To
163
- change that behavior use the
164
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setCamelizedAttributes `
165
- method on the normalizer definition::
166
-
167
- $encoder = new JsonEncoder();
168
- $normalizer = new GetSetMethodNormalizer();
169
- $normalizer->setCamelizedAttributes(array('first_name'));
170
-
171
- $serializer = new Serializer(array($normalizer), array($encoder));
172
-
173
- $json = <<<EOT
174
- {
175
- "name": "foo",
176
- "age": "19",
177
- "first_name": "bar"
178
- }
179
- EOT;
180
-
181
- $person = $serializer->deserialize($json, 'Acme\Person', 'json');
182
-
183
- As a final result, the deserializer uses the ``first_name `` attribute as if
184
- it were ``firstName `` and uses the ``getFirstName `` and ``setFirstName `` methods.
185
-
186
- Using Callbacks to Serialize Properties with Object Instances
187
- -------------------------------------------------------------
188
-
189
- When serializing, you can set a callback to format a specific object property::
190
-
191
- use Acme\Person;
192
- use Symfony\Component\Serializer\Encoder\JsonEncoder;
193
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
194
- use Symfony\Component\Serializer\Serializer;
195
-
196
- $encoder = new JsonEncoder();
197
- $normalizer = new GetSetMethodNormalizer();
198
-
199
- $callback = function ($dateTime) {
200
- return $dateTime instanceof \DateTime
201
- ? $dateTime->format(\DateTime::ISO8601)
202
- : '';
203
- };
204
-
205
- $normalizer->setCallbacks(array('createdAt' => $callback));
206
-
207
- $serializer = new Serializer(array($normalizer), array($encoder));
208
-
209
- $person = new Person();
210
- $person->setName('cordoval');
211
- $person->setAge(34);
212
- $person->setCreatedAt(new \DateTime('now'));
140
+ Go further
141
+ ----------
213
142
214
- $serializer->serialize($person, 'json');
215
- // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
143
+ If this is not already done, you should take a look at :doc: ` /components/serializer/encoders `
144
+ and :doc: ` /components/serializer/normalizers ` to be able to use the entire abilities of this component.
216
145
217
146
JMSSerializer
218
147
-------------
0 commit comments