Skip to content

Commit 047b9fe

Browse files
committed
[#2270] Tweaks to new serializer features in 2.3
1 parent a66f99b commit 047b9fe

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

components/serializer.rst

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ which Encoders and Normalizer are going to be available::
5050

5151
$serializer = new Serializer($normalizers, $encoders);
5252

53-
Serializing an object
54-
~~~~~~~~~~~~~~~~~~~~~
53+
Serializing an Object
54+
---------------------
5555

5656
For the sake of this example, assume the following class already
5757
exists in your project::
@@ -103,10 +103,17 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
103103
is the object to be serialized and the second is used to choose the proper encoder,
104104
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.
105105

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
108115
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
109-
method on normalizer definition::
116+
method on the normalizer definition::
110117

111118
use Symfony\Component\Serializer\Serializer;
112119
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -120,7 +127,7 @@ method on normalizer definition::
120127
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
121128

122129
Deserializing an Object
123-
~~~~~~~~~~~~~~~~~~~~~~~
130+
-----------------------
124131

125132
Let's see now how to do the exactly the opposite. This time, the information
126133
of the `People` class would be encoded in XML format::
@@ -141,31 +148,38 @@ needs three parameters:
141148
2. The name of the class this information will be decoded to
142149
3. The encoder used to convert that information into an array
143150

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
148162
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes`
149-
on normalizer definition::
163+
method on the normalizer definition::
150164

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'));
154168

155-
$serializer = new Serializer(array($normalizer), array($encoder));
169+
$serializer = new Serializer(array($normalizer), array($encoder));
156170

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;
164178

165-
$person = $serializer->deserialize($json, 'Acme\Person', 'json');
179+
$person = $serializer->deserialize($json, 'Acme\Person', 'json');
166180

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.
169183

170184
JMSSerializer
171185
-------------

0 commit comments

Comments
 (0)