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

Commit2257b5d

Browse files
committed
Reorganize the Serializer documentation
1 parent6a55732 commit2257b5d

File tree

7 files changed

+41
-90
lines changed

7 files changed

+41
-90
lines changed

‎components/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The Components
2525
property_access/index
2626
routing/index
2727
security/index
28-
serializer
28+
serializer/index
2929
stopwatch
3030
templating/index
3131
translation/index

‎components/map.rst.inc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
* :doc:`/components/using_components`
22

33
* :doc:`/components/browser_kit/index`
4-
4+
55
* :doc:`/components/browser_kit/introduction`
66

77
* :doc:`/components/class_loader/index`
@@ -121,9 +121,11 @@
121121
* :doc:`/components/security/authorization`
122122
* :doc:`/components/security/secure_tools`
123123

124-
***Serializer**
124+
*:doc:`/components/serializer/index`
125125

126-
* :doc:`/components/serializer`
126+
* :doc:`/components/serializer/introduction`
127+
* :doc:`/components/serializer/encoders`
128+
* :doc:`/components/serializer/normalizers`
127129

128130
***Stopwatch**
129131

‎components/serializer/encoders.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
..index::
2+
single: Serializer, Encoders
3+
4+
Encoders
5+
========

‎components/serializer/index.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Serializer
2+
==========
3+
4+
..toctree::
5+
:maxdepth:2
6+
7+
introduction
8+
encoders
9+
normalizers

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

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,20 @@ which Encoders and Normalizer are going to be available::
4747
use Symfony\Component\Serializer\Encoder\JsonEncoder;
4848
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
4949

50+
// Allows to serialize/deserialize in JSON and XML
5051
$encoders = array(new XmlEncoder(), new JsonEncoder());
52+
// Allows to normalize objects thanks to their getters and setters
5153
$normalizers = array(new GetSetMethodNormalizer());
5254

5355
$serializer = new Serializer($normalizers, $encoders);
5456

57+
The following examples assume that you instantiate the serializer as above.
58+
59+
..note::
60+
61+
Read the dedicated sections to learn more about:doc:`/components/serializer/encoders`
62+
and:doc:`/components/serializer/normalizers`.
63+
5564
Serializing an Object
5665
---------------------
5766

@@ -65,7 +74,7 @@ exists in your project::
6574
private $age;
6675
private $name;
6776

68-
//Getters
77+
//Getter
6978
public function getName()
7079
{
7180
return $this->name;
@@ -105,29 +114,6 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
105114
is the object to be serialized and the second is used to choose the proper encoder,
106115
in this case:class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`.
107116

108-
Ignoring Attributes when Serializing
109-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110-
111-
..versionadded::2.3
112-
The:method:`GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes>`
113-
method was introduced in Symfony 2.3.
114-
115-
As an option, there's a way to ignore attributes from the origin object when
116-
serializing. To remove those attributes use the
117-
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
118-
method on the normalizer definition::
119-
120-
use Symfony\Component\Serializer\Serializer;
121-
use Symfony\Component\Serializer\Encoder\JsonEncoder;
122-
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
123-
124-
$normalizer = new GetSetMethodNormalizer();
125-
$normalizer->setIgnoredAttributes(array('age'));
126-
$encoder = new JsonEncoder();
127-
128-
$serializer = new Serializer(array($normalizer), array($encoder));
129-
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
130-
131117
Deserializing an Object
132118
-----------------------
133119

@@ -141,6 +127,7 @@ of the ``Person`` class would be encoded in XML format::
141127
</person>
142128
EOF;
143129

130+
// Will return an instance of Acme\Person
144131
$person = $serializer->deserialize($data, 'Acme\Person', 'xml');
145132

146133
In this case,:method:`Symfony\\Component\\Serializer\\Serializer::deserialize`
@@ -150,69 +137,11 @@ needs three parameters:
150137
#. The name of the class this information will be decoded to
151138
#. The encoder used to convert that information into an array
152139

153-
Using Camelized Method Names for Underscored Attributes
154-
-------------------------------------------------------
155-
156-
..versionadded::2.3
157-
The:method:`GetSetMethodNormalizer::setCamelizedAttributes<Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes>`
158-
method was introduced in Symfony 2.3.
159-
160-
Sometimes property names from the serialized content are underscored (e.g.
161-
``first_name``). Normally, these attributes will use get/set methods like
162-
``getFirst_name``, when ``getFirstName`` method is what you really want. To
163-
change that behavior use the
164-
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes`
165-
method on the normalizer definition::
166-
167-
$encoder = new JsonEncoder();
168-
$normalizer = new GetSetMethodNormalizer();
169-
$normalizer->setCamelizedAttributes(array('first_name'));
170-
171-
$serializer = new Serializer(array($normalizer), array($encoder));
172-
173-
$json = <<<EOT
174-
{
175-
"name": "foo",
176-
"age": "19",
177-
"first_name": "bar"
178-
}
179-
EOT;
180-
181-
$person = $serializer->deserialize($json, 'Acme\Person', 'json');
182-
183-
As a final result, the deserializer uses the ``first_name`` attribute as if
184-
it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods.
185-
186-
Using Callbacks to Serialize Properties with Object Instances
187-
-------------------------------------------------------------
188-
189-
When serializing, you can set a callback to format a specific object property::
190-
191-
use Acme\Person;
192-
use Symfony\Component\Serializer\Encoder\JsonEncoder;
193-
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
194-
use Symfony\Component\Serializer\Serializer;
195-
196-
$encoder = new JsonEncoder();
197-
$normalizer = new GetSetMethodNormalizer();
198-
199-
$callback = function ($dateTime) {
200-
return $dateTime instanceof \DateTime
201-
? $dateTime->format(\DateTime::ISO8601)
202-
: '';
203-
};
204-
205-
$normalizer->setCallbacks(array('createdAt' => $callback));
206-
207-
$serializer = new Serializer(array($normalizer), array($encoder));
208-
209-
$person = new Person();
210-
$person->setName('cordoval');
211-
$person->setAge(34);
212-
$person->setCreatedAt(new \DateTime('now'));
140+
Go further
141+
----------
213142

214-
$serializer->serialize($person, 'json');
215-
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
143+
If this is not already done, you should take a look at:doc:`/components/serializer/encoders`
144+
and:doc:`/components/serializer/normalizers` to be able to use the entire abilities of this component.
216145

217146
JMSSerializer
218147
-------------
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
..index::
2+
single: Security, Normalizers
3+
4+
Normalizers
5+
===========

‎redirection_map‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/components/routing /components/routing/introduction
2525
/cookbook/console/generating_urls /cookbook/console/sending_emails
2626
/components/yaml /components/yaml/introduction
27+
/components/serializer /components/serializer/introduction
2728
/components/templating /components/templating/introduction
2829
/cookbook/upgrading /cookbook/upgrade/index
2930
/cookbook/security/voters_data_permission /cookbook/security/voters

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp