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

Commitdbdbf68

Browse files
committed
[Serializer] Document the encoders
1 parent379616a commitdbdbf68

File tree

12 files changed

+200
-16
lines changed

12 files changed

+200
-16
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: 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
---------------------

‎cookbook/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The Cookbook
2626
request/index
2727
routing/index
2828
security/index
29-
serializer
29+
serializer/index
3030
service_container/index
3131
session/index
3232
symfony1

‎cookbook/map.rst.inc‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@
187187
* :doc:`/cookbook/security/securing_services`
188188
* :doc:`/cookbook/security/access_control`
189189

190-
***Serializer**
190+
*:doc:`/cookbook/serializer/index`
191191

192-
* :doc:`/cookbook/serializer`
192+
* :doc:`/cookbook/serializer/introduction`
193+
* :doc:`/cookbook/serializer/custom_encoders`
193194

194195
* :doc:`/cookbook/service_container/index`
195196

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

‎cookbook/serializer.rst‎renamed to ‎cookbook/serializer/introduction.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
..index::
2-
single: Serializer
2+
single: Serializer; Introduction
33

44
How to Use the Serializer
55
=========================
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
-------------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp