Skip to content

[WCM] Added documentation to cover PR #6951 on serializer component. #2270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
is the object to be serialized and the second is used to choose the proper encoder,
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.

As an option, there's a way to ignore attributes from the origin object to be
serialized, to remove those attributes use
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
method on normalizer definition::

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;

$normalizer = new GetSetMethodNormalizer();
$normalizer->setIgnoredAttributes(array('age'));
$encoder = new JsonEncoder();

$serializer = new Serializer(array($normalizer), array($encoder));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot the use statement for this class

$serializer->serialize($person, 'json'); // Output: {"name":"foo"}

Deserializing an Object
~~~~~~~~~~~~~~~~~~~~~~~

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

Sometimes property names from the serialized content are underscored, in a
regular configuration those attributes will use get/set methods as
``getCamel_case``, when ``getCamelCase`` method is preferable. To change that
behavior use
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes`
on normalizer definition::

$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$normalizer->setCamelizedAttributes(array('camel_case'));

$serializer = new Serializer(array($normalizer), array($encoder));

$json = <<<EOT
{
"name": "foo",
"age": "19",
"camel_case": "bar"
}
EOT;

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

As a final result, Person object uses ``camelCase`` attribute for
``camel_case`` json parameter, the same applies on getters and setters.

JMSSerializer
-------------

Expand Down