Skip to content

[Component][Serializer] Adding a brand new chapter on the Serializer Component #1684

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
116 changes: 116 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
.. index::
single: Serializer
single: Components; Serializer

The Serializer Component
========================

The Serializer Component is meant to be used to turn Objects into a
specific format (XML, JSON, Yaml, ...) and the other way around.

In order to do so, the Serializer Components follows the following
simple schema.

.. image:: /images/components/serializer/serializer_workflow.png

As you can see in the picture above, an array is used as a man in
the middle. This way, Normalizers will only deal with turning specific
**formats** into **arrays** and vice versa. The same way, Normalizers
will deal with turning specific **objects** into **arrays** and vice versa.

Installation
------------

You can install the component in many different ways:

* Use the official Git repository (https://github.com/symfony/Serializer);
* Install it via PEAR ( `pear.symfony.com/Serializer`);
* Install it via Composer (`symfony/serializer` on Packagist).

Usage
-----

Using the Serializer component is really simple. We just need to set up
the :class:`Symfony\\Component\\Serializer\\Serializer` specifying
what Encoders and Normalizer are going to be available::

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

$encoders = array(new XmlEncoder(), new JsonEncoder() );
Copy link
Member

Choose a reason for hiding this comment

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

You should remove the space between JsonEncoder() and ); (and maybe put this on 3 lines with a comma after each item)

$normalizers = array(new GetSetMethodNormalizer());

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


Serializing an object
~~~~~~~~~~~~~~~~~~~~~

For the sake of this example, let's ssume the following class already
exists in our project::

namespace Acme;

class Person
{
private $age;
private $name;

// Getters
public function getName()
{
return $this->name;
}

public function getAge()
{
return $this->age;
}

// Setters
public function setName($name)
{
$this->name = $name;
}

public function setAge($age)
{
$this->age = $age;
}
}

Now, if we want to serialize this object into JSON, we only need to
use the Serializer service created before::

$person = new Acme\Person();
$person->setName('foo');
$person->setAge(99);

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

The first paramater of the `Serializer::serialize` is the object to be
serialized, the second one will be used to pick the proper encoder,
in this case :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.

Deserializing an object
~~~~~~~~~~~~~~~~~~~~~~~~

Let's see now how to do the exactly the opposite. This time, the information
of the `People` class would be encoded in XML format::

$data = <<<EOF
<person>
<name>foo</name>
<age>99</age>
</person>
EOF;

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

In this case, `Serializer::deserialize` needs three parameters:

1. The information to be decoded
2. The name of the class this information will be decoded to
3. The encoder used to convert that information into an array
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.