Skip to content

Commit 2521b05

Browse files
committed
Added documentation to cover PR #6951 on serializer component.
1 parent 5246f51 commit 2521b05

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
@@ -96,6 +96,22 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
9696
is the object to be serialized and the second is used to choose the proper encoder,
9797
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.
9898

99+
As an option, there's a way to ignore attributes from the origin object to be
100+
serialized, to remove those attributes use
101+
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
102+
method on normalizer definition::
103+
104+
use Symfony\Component\Serializer\Serializer;
105+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
106+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
107+
108+
$normalizer = new GetSetMethodNormalizer();
109+
$normalizer->setIgnoredAttributes(array('age'));
110+
$encoder = new JsonEncoder();
111+
112+
$serializer = new Serializer(array($normalizer), array($encoder));
113+
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
114+
99115
Deserializing an Object
100116
~~~~~~~~~~~~~~~~~~~~~~~
101117

@@ -118,6 +134,32 @@ needs three parameters:
118134
2. The name of the class this information will be decoded to
119135
3. The encoder used to convert that information into an array
120136

137+
Sometimes property names from the serialized content are underscored, in a
138+
regular configuration those attributes will use get/set methods as
139+
``getCamel_case``, when ``getCamelCase`` method is preferable. To change that
140+
behavior use
141+
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes`
142+
on normalizer definition::
143+
144+
$encoder = new JsonEncoder();
145+
$normalizer = new GetSetMethodNormalizer();
146+
$normalizer->setCamelizedAttributes(array('camel_case'));
147+
148+
$serializer = new Serializer(array($normalizer), array($encoder));
149+
150+
$json = <<<EOT
151+
{
152+
"name": "foo",
153+
"age": "19",
154+
"camel_case": "bar"
155+
}
156+
EOT;
157+
158+
$person = $serializer->deserialize($json, 'Acme\Person', 'json');
159+
160+
As a final result, Person object uses ``camelCase`` attribute for
161+
``camel_case`` json parameter, the same applies on getters and setters.
162+
121163
JMSSerializer
122164
-------------
123165

0 commit comments

Comments
 (0)