Skip to content

Commit a66f99b

Browse files
committed
Merge pull request #2270 from marcosQuesada/serializer/denormalize-camelcase
[WCM] Added documentation to cover PR #6951 on serializer component.
2 parents 6b087ee + 2521b05 commit a66f99b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

components/serializer.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ 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
108+
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
109+
method on normalizer definition::
110+
111+
use Symfony\Component\Serializer\Serializer;
112+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
113+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
114+
115+
$normalizer = new GetSetMethodNormalizer();
116+
$normalizer->setIgnoredAttributes(array('age'));
117+
$encoder = new JsonEncoder();
118+
119+
$serializer = new Serializer(array($normalizer), array($encoder));
120+
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
121+
106122
Deserializing an Object
107123
~~~~~~~~~~~~~~~~~~~~~~~
108124

@@ -125,6 +141,32 @@ needs three parameters:
125141
2. The name of the class this information will be decoded to
126142
3. The encoder used to convert that information into an array
127143

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
148+
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes`
149+
on normalizer definition::
150+
151+
$encoder = new JsonEncoder();
152+
$normalizer = new GetSetMethodNormalizer();
153+
$normalizer->setCamelizedAttributes(array('camel_case'));
154+
155+
$serializer = new Serializer(array($normalizer), array($encoder));
156+
157+
$json = <<<EOT
158+
{
159+
"name": "foo",
160+
"age": "19",
161+
"camel_case": "bar"
162+
}
163+
EOT;
164+
165+
$person = $serializer->deserialize($json, 'Acme\Person', 'json');
166+
167+
As a final result, Person object uses ``camelCase`` attribute for
168+
``camel_case`` json parameter, the same applies on getters and setters.
169+
128170
JMSSerializer
129171
-------------
130172

0 commit comments

Comments
 (0)