Skip to content

Commit b669e8a

Browse files
committed
feature #6976 [Finishing][Serializer] Document the encoders (Ener-Getick, weaverryan)
This PR was merged into the 2.7 branch. Discussion ---------- [Finishing][Serializer] Document the encoders Finishing #6684 - just opening the PR to review and run tests. Commits ------- 10b2fa3 Minor language tweaks f99663a removing cookbook entries 73172c6 updating links 33e5b76 Moving files into the new structure 2c66c83 Merge branch 'ENCODERS' of https://github.com/Ener-Getick/symfony-docs into Ener-Getick-ENCODERS dbdbf68 [Serializer] Document the encoders
2 parents 3c1ae4a + 10b2fa3 commit b669e8a

File tree

4 files changed

+172
-7
lines changed

4 files changed

+172
-7
lines changed

components/serializer.rst

Lines changed: 4 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 normalizer are going to be available::
4848

4949
use Symfony\Component\Serializer\Serializer;
5050
use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -57,10 +57,9 @@ 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``.
6463

6564
Serializing an Object
6665
---------------------

serializer.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ A service leveraging `APCu`_ (and APC for PHP < 5.5) is built-in.
226226
),
227227
));
228228
229-
Going Further with the Serializer Component
230-
-------------------------------------------
229+
Going Further with the Serializer
230+
---------------------------------
231231

232232
`ApiPlatform`_ provides an API system supporting `JSON-LD`_ and `Hydra Core Vocabulary`_
233233
hypermedia formats. It is built on top of the Symfony Framework and its Serializer
@@ -237,6 +237,12 @@ and a caching system.
237237
If you want to leverage the full power of the Symfony Serializer component,
238238
take a look at how this bundle works.
239239

240+
.. toctree::
241+
:maxdepth: 1
242+
:glob:
243+
244+
serializer/*
245+
240246
.. _`APCu`: https://github.com/krakjoe/apcu
241247
.. _`ApiPlatform`: https://github.com/api-platform/core
242248
.. _`JSON-LD`: http://json-ld.org

serializer/custom_encoders.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. index::
2+
single: Serializer; Custom encoders
3+
4+
How to Create your Custom Encoder
5+
=================================
6+
7+
The :doc:`Serializer Component </components/serializer>` uses Normalizers
8+
to transform any data to an array. Then, by leveraging *Encoders*, that data can
9+
be convereted into any data-structure (e.g. JSON).
10+
11+
The Component provides several built-in encoders that are described
12+
:doc:`in their own section </serializer/encoders>` but you may want
13+
to use another structure that's not supported.
14+
15+
Creating a new encoder
16+
----------------------
17+
18+
Imagine you want to serialize and deserialize Yaml. For that you'll have to
19+
create your own encoder that uses the
20+
:doc:`Yaml Component </components/yaml>`::
21+
22+
namespace AppBundle\Encoder;
23+
24+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
25+
use Symfony\Component\Serializer\Encoder\EncoderInterface;
26+
use Symfony\Component\Yaml\Yaml;
27+
28+
class YamlEncoder implements EncoderInterface, DecoderInterface
29+
{
30+
public function encode($data, $format, array $context = array())
31+
{
32+
return Yaml::dump($data);
33+
}
34+
35+
public function supportsEncoding($format)
36+
{
37+
return 'yaml' === $format;
38+
}
39+
40+
public function decode($data, $format, array $context = array())
41+
{
42+
return Yaml::parse($data);
43+
}
44+
45+
public function supportsDecoding($format)
46+
{
47+
return 'yaml' === $format;
48+
}
49+
}
50+
51+
Registering it in your app
52+
--------------------------
53+
54+
If you use the Symfony Framework. then you probably want to register this encoder
55+
as a service in your app. Then, you only need to tag it with ``serializer.encoder``
56+
to inject your custom encoder into the Serializer.
57+
58+
.. configuration-block::
59+
60+
.. code-block:: yaml
61+
62+
# app/config/services.yml
63+
services:
64+
app.encoder.yaml:
65+
class: AppBundle\Encoder\YamlEncoder
66+
tags:
67+
- { name: serializer.encoder }
68+
69+
.. code-block:: xml
70+
71+
<!-- app/config/services.xml -->
72+
<?xml version="1.0" encoding="UTF-8" ?>
73+
<container xmlns="http://symfony.com/schema/dic/services"
74+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
76+
77+
<services>
78+
<service id="app.encoder.yaml" class="AppBundle\Encoder\YamlEncoder">
79+
<tag name="serializer.encoder" />
80+
</service>
81+
</services>
82+
</container>
83+
84+
.. code-block:: php
85+
86+
// app/config/services.php
87+
$container
88+
->register(
89+
'app.encoder.yaml',
90+
'AppBundle\Encoder\YamlEncoder'
91+
)
92+
->addTag('serializer.encoder')
93+
;
94+
95+
Now you'll be able to serialize and deserialize Yaml!
96+
97+
.. _tracker: https://github.com/symfony/symfony/issues

serializer/encoders.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 transforms 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
63+
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.

0 commit comments

Comments
 (0)