Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c66c83

Browse files
committed
Merge branch 'ENCODERS' ofhttps://github.com/Ener-Getick/symfony-docs into Ener-Getick-ENCODERS
* 'ENCODERS' ofhttps://github.com/Ener-Getick/symfony-docs: [Serializer] Document the encoders
2 parents3c1ae4a +dbdbf68 commit2c66c83

File tree

6 files changed

+426
-5
lines changed

6 files changed

+426
-5
lines changed

‎components/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.

‎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-
whichEncoders andNormalizer are going to be available::
47+
whichencoders andnormalizers 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
---------------------
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/index>` uses Normalizers
8+
to transform any data to an array that can be then converted in whatever
9+
data-structured language you want thanks to Encoders.
10+
11+
The Component provides several built-in encoders that are described
12+
:doc:`in their own section</components/serializer/encoders>` but you may want
13+
to use another language 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 may use the
20+
:doc:`Yaml Component</components/yaml/index>`::
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 as `serializer.encoder` and it will be
56+
injected in 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+
<containerxmlns="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+
<serviceid="app.encoder.yaml"class="AppBundle\Encoder\YamlEncoder">
79+
<tagname="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

‎cookbook/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+
custom_encoders

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp