Skip to content

Commit 649bcce

Browse files
committed
[Serializer] Document the encoders
1 parent 09a3381 commit 649bcce

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

components/index.rst

+1-1
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

+3-2
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

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

components/serializer/index.rst

+8
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

+7-3
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;
@@ -58,10 +58,14 @@ which Encoders and Normalizer are going to be available::
5858

5959
The preferred normalizer is the
6060
: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
61+
normalizers are available. All
6362
the examples shown below use the ``ObjectNormalizer``.
6463

64+
.. note::
65+
66+
Read the dedicated sections to learn more about :doc:`/components/serializer/encoders`
67+
and `Normalizers`_.
68+
6569
Serializing an Object
6670
---------------------
6771

cookbook/serializer.rst

+2-2
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

+1
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)