@@ -16,9 +16,9 @@ simple schema.
16
16
17
17
.. image :: /_images/components/serializer/serializer_workflow.png
18
18
19
- As you can see in the picture above, an array is used as a man in
20
- the middle . This way, Encoders will only deal with turning specific
21
- **formats ** into **arrays ** and vice versa. The same way, Normalizers
19
+ As you can see in the picture above, an array is used as an intermediary between
20
+ objects and serialized contents . This way, encoders will only deal with turning
21
+ specific **formats ** into **arrays ** and vice versa. The same way, Normalizers
22
22
will deal with turning specific **objects ** into **arrays ** and vice versa.
23
23
24
24
Serialization is a complex topic. This component may not cover all your use cases out of the box,
@@ -66,13 +66,13 @@ Serializing an Object
66
66
For the sake of this example, assume the following class already
67
67
exists in your project::
68
68
69
- namespace Acme ;
69
+ namespace App\Model ;
70
70
71
71
class Person
72
72
{
73
73
private $age;
74
74
private $name;
75
- private $sportsman ;
75
+ private $sportsperson ;
76
76
private $createdAt;
77
77
78
78
// Getters
@@ -92,9 +92,9 @@ exists in your project::
92
92
}
93
93
94
94
// Issers
95
- public function isSportsman ()
95
+ public function isSportsperson ()
96
96
{
97
- return $this->sportsman ;
97
+ return $this->sportsperson ;
98
98
}
99
99
100
100
// Setters
@@ -108,9 +108,9 @@ exists in your project::
108
108
$this->age = $age;
109
109
}
110
110
111
- public function setSportsman($sportsman )
111
+ public function setSportsperson($sportsperson )
112
112
{
113
- $this->sportsman = $sportsman ;
113
+ $this->sportsperson = $sportsperson ;
114
114
}
115
115
116
116
public function setCreatedAt($createdAt)
@@ -122,14 +122,14 @@ exists in your project::
122
122
Now, if you want to serialize this object into JSON, you only need to
123
123
use the Serializer service created before::
124
124
125
- $person = new Acme \Person();
125
+ $person = new App\Model \Person();
126
126
$person->setName('foo');
127
127
$person->setAge(99);
128
- $person->setSportsman (false);
128
+ $person->setSportsperson (false);
129
129
130
130
$jsonContent = $serializer->serialize($person, 'json');
131
131
132
- // $jsonContent contains {"name":"foo","age":99,"sportsman ":false}
132
+ // $jsonContent contains {"name":"foo","age":99,"sportsperson ":false}
133
133
134
134
echo $jsonContent; // or return it in a Response
135
135
@@ -143,13 +143,13 @@ Deserializing an Object
143
143
You'll now learn how to do the exact opposite. This time, the information
144
144
of the ``Person `` class would be encoded in XML format::
145
145
146
- use Acme \Person;
146
+ use App\Model \Person;
147
147
148
148
$data = <<<EOF
149
149
<person>
150
150
<name>foo</name>
151
151
<age>99</age>
152
- <sportsman >false</sportsman >
152
+ <sportsperson >false</sportsperson >
153
153
</person>
154
154
EOF;
155
155
@@ -171,7 +171,7 @@ The serializer can also be used to update an existing object::
171
171
$person = new Person();
172
172
$person->setName('bar');
173
173
$person->setAge(99);
174
- $person->setSportsman (true);
174
+ $person->setSportsperson (true);
175
175
176
176
$data = <<<EOF
177
177
<person>
@@ -181,7 +181,7 @@ The serializer can also be used to update an existing object::
181
181
EOF;
182
182
183
183
$serializer->deserialize($data, Person::class, 'xml', array('object_to_populate' => $person));
184
- // $person = Acme\ Person(name: 'foo', age: '69', sportsman : true)
184
+ // $person = App\Model\ Person(name: 'foo', age: '69', sportsperson : true)
185
185
186
186
This is a common need when working with an ORM.
187
187
@@ -356,7 +356,7 @@ method on the normalizer definition::
356
356
$encoder = new JsonEncoder();
357
357
358
358
$serializer = new Serializer(array($normalizer), array($encoder));
359
- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman ":false}
359
+ $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson ":false}
360
360
361
361
Converting Property Names when Serializing and Deserializing
362
362
------------------------------------------------------------
@@ -474,8 +474,8 @@ Serializing Boolean Attributes
474
474
------------------------------
475
475
476
476
If you are using isser methods (methods prefixed by ``is ``, like
477
- ``Acme\ Person::isSportsman () ``), the Serializer component will automatically
478
- detect and use it to serialize related attributes.
477
+ ``App\Model\ Person::isSportsperson () ``), the Serializer component will
478
+ automatically detect and use it to serialize related attributes.
479
479
480
480
The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
481
481
and ``remove ``.
@@ -485,7 +485,7 @@ Using Callbacks to Serialize Properties with Object Instances
485
485
486
486
When serializing, you can set a callback to format a specific object property::
487
487
488
- use Acme \Person;
488
+ use App\Model \Person;
489
489
use Symfony\Component\Serializer\Encoder\JsonEncoder;
490
490
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
491
491
use Symfony\Component\Serializer\Serializer;
0 commit comments