@@ -50,8 +50,8 @@ which Encoders and Normalizer are going to be available::
50
50
51
51
$serializer = new Serializer($normalizers, $encoders);
52
52
53
- Serializing an object
54
- ~~~~~~~~~~~~~~~~~~~~~
53
+ Serializing an Object
54
+ ---------------------
55
55
56
56
For the sake of this example, assume the following class already
57
57
exists in your project::
@@ -103,10 +103,17 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
103
103
is the object to be serialized and the second is used to choose the proper encoder,
104
104
in this case :class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `.
105
105
106
- As an option, there's a way to ignore attributes from the origin object to be
107
- serialized, to remove those attributes use
106
+ Ignoring Attributes when Serializing
107
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
+
109
+ .. versionadded :: 2.3
110
+ The :method: `GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes> `
111
+ method was added in Symfony 2.3.
112
+
113
+ As an option, there's a way to ignore attributes from the origin object when
114
+ serializing. To remove those attributes use the
108
115
:method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
109
- method on normalizer definition::
116
+ method on the normalizer definition::
110
117
111
118
use Symfony\Component\Serializer\Serializer;
112
119
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -120,7 +127,7 @@ method on normalizer definition::
120
127
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
121
128
122
129
Deserializing an Object
123
- ~~~~~~~~~~~~~~~~~~~~~~~
130
+ -----------------------
124
131
125
132
Let's see now how to do the exactly the opposite. This time, the information
126
133
of the `People ` class would be encoded in XML format::
@@ -141,31 +148,38 @@ needs three parameters:
141
148
2. The name of the class this information will be decoded to
142
149
3. The encoder used to convert that information into an array
143
150
144
- Sometimes property names from the serialized content are underscored, in a
145
- regular configuration those attributes will use get/set methods as
146
- ``getCamel_case ``, when ``getCamelCase `` method is preferable. To change that
147
- behavior use
151
+ Using Camelized Method Names for Underscored Attributes
152
+ -------------------------------------------------------
153
+
154
+ .. versionadded :: 2.3
155
+ The :method: `GetSetMethodNormalizer::setCamelizedAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setCamelizedAttributes> `
156
+ method was added in Symfony 2.3.
157
+
158
+ Sometimes property names from the serialized content are underscored (e.g.
159
+ ``first_name ``). Normally, these attributes will use get/set methods like
160
+ ``getFirst_name ``, when ``getFirstName `` method is what you really want. To
161
+ change that behavior use the
148
162
:method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setCamelizedAttributes `
149
- on normalizer definition::
163
+ method on the normalizer definition::
150
164
151
- $encoder = new JsonEncoder();
152
- $normalizer = new GetSetMethodNormalizer();
153
- $normalizer->setCamelizedAttributes(array('camel_case '));
165
+ $encoder = new JsonEncoder();
166
+ $normalizer = new GetSetMethodNormalizer();
167
+ $normalizer->setCamelizedAttributes(array('first_name '));
154
168
155
- $serializer = new Serializer(array($normalizer), array($encoder));
169
+ $serializer = new Serializer(array($normalizer), array($encoder));
156
170
157
- $json = <<<EOT
158
- {
159
- "name": "foo",
160
- "age": "19",
161
- "camel_case ": "bar"
162
- }
163
- EOT;
171
+ $json = <<<EOT
172
+ {
173
+ "name": "foo",
174
+ "age": "19",
175
+ "first_name ": "bar"
176
+ }
177
+ EOT;
164
178
165
- $person = $serializer->deserialize($json, 'Acme\Person', 'json');
179
+ $person = $serializer->deserialize($json, 'Acme\Person', 'json');
166
180
167
- As a final result, Person object uses `` camelCase `` attribute for
168
- `` camel_case `` json parameter, the same applies on getters and setters .
181
+ As a final result, the deserializer uses the `` first_name `` attribute as if
182
+ it were `` firstName `` and uses the `` getFirstName `` and `` setFirstName `` methods .
169
183
170
184
JMSSerializer
171
185
-------------
0 commit comments