Skip to content

Commit 0cf7ef3

Browse files
committed
[Serializer] Document the encoders
1 parent 09a3381 commit 0cf7ef3

File tree

7 files changed

+130
-10
lines changed

7 files changed

+130
-10
lines changed

components/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The Components
2828
property_access/index
2929
routing/index
3030
security/index
31-
serializer
31+
serializer/index
3232
stopwatch
3333
templating/index
3434
translation/index

components/map.rst.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@
140140
* :doc:`/components/security/authorization`
141141
* :doc:`/components/security/secure_tools`
142142

143-
* **Serializer**
143+
* :doc:`/components/serializer/index`
144144

145-
* :doc:`/components/serializer`
145+
* :doc:`/components/serializer/introduction`
146+
* :doc:`/components/serializer/encoders`
146147

147148
* **Stopwatch**
148149

components/serializer/encoders.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
.. index::
2+
single: Serializer, Encoders
3+
4+
Encoders
5+
========
6+
7+
Encoders basically turn **arrays** into **formats** and vice versa.
8+
They implement
9+
:class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for
10+
encoding (array to format) and
11+
:class:`Symfony\\Component\\Serializer\\Encoder\\DecoderInterface` for
12+
decoding (format to array).
13+
14+
You can add new encoders to a Serializer instance by using its second constructor argument::
15+
16+
use Symfony\Component\Serializer\Serializer;
17+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
18+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
19+
20+
$encoders = array(new XmlEncoder(), new JsonEncoder());
21+
$serializer = new Serializer(array(), $encoders);
22+
23+
Built-in encoders
24+
-----------------
25+
26+
Two encoders are used in the example above:
27+
28+
* :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML
29+
* :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON
30+
31+
The ``XmlEncoder``
32+
~~~~~~~~~~~~~~~~~~
33+
34+
This encoder transform arrays into XML and vice versa.
35+
36+
For example, take an object normalized as following::
37+
38+
array('foo' => array(1, 2), 'bar' => true);
39+
40+
The ``XmlEncoder`` will encode this object like that::
41+
42+
<?xml version="1.0"?>
43+
<response>
44+
<foo>1</foo>
45+
<foo>2</foo>
46+
<bar>1</bar>
47+
</response>
48+
49+
Be aware that this encoder will consider keys beginning with ``@`` as attributes::
50+
51+
$encoder = new XmlEncoder();
52+
$encoder->encode(array('foo' => array('@bar' => 'value')));
53+
// will return:
54+
// <?xml version="1.0"?>
55+
// <response>
56+
// <foo bar="value" />
57+
// </response>
58+
59+
The ``JsonEncoder``
60+
~~~~~~~~~~~~~~~~~~~
61+
62+
The ``JsonEncoder`` is much simpler and is based on the PHP :phpfunction:`json_encode` and :phpfunction:`json_decode` functions.
63+
64+
Custom Encoders
65+
---------------
66+
67+
If you need to support another format than XML and JSON, you can create your own encoder.
68+
You might want to serialize and deserialize Yaml. For that, you can use
69+
:doc:`/components/yaml/index`::
70+
71+
namespace App\Encoder;
72+
73+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
74+
use Symfony\Component\Serializer\Encoder\EncoderInterface;
75+
use Symfony\Component\Yaml\Yaml;
76+
77+
class YamlEncoder implements EncoderInterface, DecoderInterface
78+
{
79+
public function encode($data, $format, array $context = array())
80+
{
81+
return Yaml::dump($data);
82+
}
83+
84+
public function supportsEncoding($format)
85+
{
86+
return 'json' === $format;
87+
}
88+
89+
public function decode($data, $format, array $context = array())
90+
{
91+
return Yaml::parse($data);
92+
}
93+
94+
public function supportsDecoding($format)
95+
{
96+
return 'json' === $format;
97+
}
98+
}
99+
100+
Then just pass it to your serializer::
101+
102+
use Symfony\Component\Serializer\Serializer;
103+
104+
$serializer = new Serializer(array(), array(new App\Encoder\YamlEncoder()));
105+
106+
Now you'll be able to serialize and deserialize Yaml.

components/serializer/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Serializer
2+
==========
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
introduction
8+
encoders

components/serializer.rst renamed to components/serializer/introduction.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Usage
4444

4545
Using the Serializer component is really simple. You just need to set up
4646
the :class:`Symfony\\Component\\Serializer\\Serializer` specifying
47-
which Encoders and Normalizer are going to be available::
47+
which encoders and normalizers are going to be available::
4848

4949
use Symfony\Component\Serializer\Serializer;
5050
use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -57,10 +57,14 @@ which Encoders and Normalizer are going to be available::
5757
$serializer = new Serializer($normalizers, $encoders);
5858

5959
The preferred normalizer is the
60-
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`, but other
61-
normalizers are available.
62-
To read more about them, refer to the `Normalizers`_ section of this page. All
63-
the examples shown below use the ``ObjectNormalizer``.
60+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`,
61+
but other normalizers are available. All the examples shown below use
62+
the ``ObjectNormalizer``.
63+
64+
.. seealso::
65+
66+
Read the dedicated sections to learn more about :doc:`/components/serializer/encoders`
67+
and `Normalizers`_.
6468

6569
Serializing an Object
6670
---------------------

cookbook/serializer.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ How to Use the Serializer
66

77
Serializing and deserializing to and from objects and different formats (e.g.
88
JSON or XML) is a very complex topic. Symfony comes with a
9-
:doc:`Serializer Component </components/serializer>`, which gives you some
9+
:doc:`Serializer Component </components/serializer/index>`, which gives you some
1010
tools that you can leverage for your solution.
1111

1212
In fact, before you start, get familiar with the serializer, normalizers
13-
and encoders by reading the :doc:`Serializer Component </components/serializer>`.
13+
and encoders by reading the :doc:`Serializer Component </components/serializer/index>`.
1414

1515
Activating the Serializer
1616
-------------------------

redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/components/yaml /components/yaml/introduction
2727
/components/templating /components/templating/introduction
2828
/components/filesystem /components/filesystem/introduction
29+
/components/serializer /components/serializer/introduction
2930
/cmf/reference/configuration/block /cmf/bundles/block/configuration
3031
/cmf/reference/configuration/content /cmf/bundles/content/configuration
3132
/cmf/reference/configuration/core /cmf/bundles/core/configuration

0 commit comments

Comments
 (0)