@@ -13,9 +13,9 @@ simple schema.
13
13
14
14
.. image :: /_images/components/serializer/serializer_workflow.png
15
15
16
- As you can see in the picture above, an array is used as a man in
17
- the middle . This way, Encoders will only deal with turning specific
18
- **formats ** into **arrays ** and vice versa. The same way, Normalizers
16
+ As you can see in the picture above, an array is used as an intermediary between
17
+ objects and serialized contents . This way, encoders will only deal with turning
18
+ specific **formats ** into **arrays ** and vice versa. The same way, Normalizers
19
19
will deal with turning specific **objects ** into **arrays ** and vice versa.
20
20
21
21
Serialization is a complex topic. This component may not cover all your use cases out of the box,
@@ -63,13 +63,13 @@ Serializing an Object
63
63
For the sake of this example, assume the following class already
64
64
exists in your project::
65
65
66
- namespace Acme ;
66
+ namespace App\Model ;
67
67
68
68
class Person
69
69
{
70
70
private $age;
71
71
private $name;
72
- private $sportsman ;
72
+ private $sportsperson ;
73
73
private $createdAt;
74
74
75
75
// Getters
@@ -89,9 +89,9 @@ exists in your project::
89
89
}
90
90
91
91
// Issers
92
- public function isSportsman ()
92
+ public function isSportsperson ()
93
93
{
94
- return $this->sportsman ;
94
+ return $this->sportsperson ;
95
95
}
96
96
97
97
// Setters
@@ -105,9 +105,9 @@ exists in your project::
105
105
$this->age = $age;
106
106
}
107
107
108
- public function setSportsman($sportsman )
108
+ public function setSportsperson($sportsperson )
109
109
{
110
- $this->sportsman = $sportsman ;
110
+ $this->sportsperson = $sportsperson ;
111
111
}
112
112
113
113
public function setCreatedAt($createdAt)
@@ -119,14 +119,14 @@ exists in your project::
119
119
Now, if you want to serialize this object into JSON, you only need to
120
120
use the Serializer service created before::
121
121
122
- $person = new Acme \Person();
122
+ $person = new App\Model \Person();
123
123
$person->setName('foo');
124
124
$person->setAge(99);
125
- $person->setSportsman (false);
125
+ $person->setSportsperson (false);
126
126
127
127
$jsonContent = $serializer->serialize($person, 'json');
128
128
129
- // $jsonContent contains {"name":"foo","age":99,"sportsman ":false}
129
+ // $jsonContent contains {"name":"foo","age":99,"sportsperson ":false}
130
130
131
131
echo $jsonContent; // or return it in a Response
132
132
@@ -140,13 +140,13 @@ Deserializing an Object
140
140
You'll now learn how to do the exact opposite. This time, the information
141
141
of the ``Person `` class would be encoded in XML format::
142
142
143
- use Acme \Person;
143
+ use App\Model \Person;
144
144
145
145
$data = <<<EOF
146
146
<person>
147
147
<name>foo</name>
148
148
<age>99</age>
149
- <sportsman >false</sportsman >
149
+ <sportsperson >false</sportsperson >
150
150
</person>
151
151
EOF;
152
152
@@ -187,7 +187,7 @@ The serializer can also be used to update an existing object::
187
187
$person = new Person();
188
188
$person->setName('bar');
189
189
$person->setAge(99);
190
- $person->setSportsman (true);
190
+ $person->setSportsperson (true);
191
191
192
192
$data = <<<EOF
193
193
<person>
@@ -197,7 +197,7 @@ The serializer can also be used to update an existing object::
197
197
EOF;
198
198
199
199
$serializer->deserialize($data, Person::class, 'xml', array('object_to_populate' => $person));
200
- // $person = Acme\ Person(name: 'foo', age: '69', sportsman : true)
200
+ // $person = App\Model\ Person(name: 'foo', age: '69', sportsperson : true)
201
201
202
202
This is a common need when working with an ORM.
203
203
@@ -400,7 +400,7 @@ method on the normalizer definition::
400
400
$encoder = new JsonEncoder();
401
401
402
402
$serializer = new Serializer(array($normalizer), array($encoder));
403
- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman ":false}
403
+ $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson ":false}
404
404
405
405
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
406
406
@@ -512,8 +512,8 @@ Serializing Boolean Attributes
512
512
------------------------------
513
513
514
514
If you are using isser methods (methods prefixed by ``is ``, like
515
- ``Acme\ Person::isSportsman () ``), the Serializer component will automatically
516
- detect and use it to serialize related attributes.
515
+ ``App\Model\ Person::isSportsperson () ``), the Serializer component will
516
+ automatically detect and use it to serialize related attributes.
517
517
518
518
The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
519
519
and ``remove ``.
@@ -523,7 +523,7 @@ Using Callbacks to Serialize Properties with Object Instances
523
523
524
524
When serializing, you can set a callback to format a specific object property::
525
525
526
- use Acme \Person;
526
+ use App\Model \Person;
527
527
use Symfony\Component\Serializer\Encoder\JsonEncoder;
528
528
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
529
529
use Symfony\Component\Serializer\Serializer;
@@ -860,7 +860,7 @@ Here, we set it to 2 for the ``$child`` property:
860
860
/**
861
861
* @MaxDepth(2)
862
862
*/
863
- public $foo ;
863
+ public $child ;
864
864
865
865
// ...
866
866
}
@@ -869,7 +869,7 @@ Here, we set it to 2 for the ``$child`` property:
869
869
870
870
Acme\MyObj :
871
871
attributes :
872
- foo :
872
+ child :
873
873
max_depth : 2
874
874
875
875
.. code-block :: xml
@@ -881,7 +881,7 @@ Here, we set it to 2 for the ``$child`` property:
881
881
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
882
882
>
883
883
<class name =" Acme\MyObj" >
884
- <attribute name =" foo " max-depth =" 2" />
884
+ <attribute name =" child " max-depth =" 2" />
885
885
</class >
886
886
</serializer >
887
887
0 commit comments