Skip to content

Commit 2d4ff3b

Browse files
committed
Document serializer encoders
1 parent 21a7840 commit 2d4ff3b

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

components/serializer/encoders.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,104 @@
33

44
Encoders
55
========
6+
7+
Encoders basically turn **arrays** into specific **formats** and vice versa.
8+
They always implement :class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for encoding (arrays to specific formats).
9+
And they implement :class:`Symfony\\Component\\Serializer\\Encoder\\DecoderInterface` for decoding (specific formats to arrays).
10+
11+
You can add new encoders to a Serializer instance by using its second constructor argument::
12+
13+
use Symfony\Component\Serializer\Serializer;
14+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
15+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16+
17+
$encoders = array(new XmlEncoder(), new JsonEncoder());
18+
$serializer = new Serializer(array(), $encoders);
19+
20+
Built-in encoders
21+
-----------------
22+
23+
You can see in the example above that we use two encoders from symfony:
24+
25+
* :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML
26+
* :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON
27+
28+
The ``XmlEncoder``
29+
~~~~~~~~~~~~~~~~~~
30+
31+
This encoder transform arrays into XML and vice versa.
32+
33+
For example, we will guess that you have an object normalized as following::
34+
35+
array('foo' => array(1, 2), 'bar' => true);
36+
37+
The ``XmlEncoder`` will encode this object like that::
38+
39+
<?xml version="1.0"?>
40+
<response>
41+
<foo>1</foo>
42+
<foo>2</foo>
43+
<bar>1</bar>
44+
</response>
45+
46+
Be aware that this encoder will consider keys beginning with ``@`` as attributes::
47+
48+
$encoder = new XmlEncoder();
49+
$encoder->encode(array('foo' => array('@bar' => 'value')));
50+
// will return:
51+
// <?xml version="1.0"?>
52+
// <response>
53+
// <foo bar="value" />
54+
// </response>
55+
56+
The ``JsonEncoder``
57+
~~~~~~~~~~~~~~~~~~~
58+
59+
The ``JsonEncoder`` is much simpler and is based on the PHP `json_encode`_ and `json_decode`_ functions.
60+
61+
.. _json_encode: https://secure.php.net/manual/fr/function.json-encode.php
62+
.. _json_decode: https://secure.php.net/manual/fr/function.json-decode.php
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+
We will guess that you want to serialize and deserialize Yaml. For that, we will 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/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ exists in your project::
7474
private $age;
7575
private $name;
7676

77-
// Getter
77+
// Getters
7878
public function getName()
7979
{
8080
return $this->name;

0 commit comments

Comments
 (0)