@@ -62,6 +62,7 @@ exists in your project::
62
62
{
63
63
private $age;
64
64
private $name;
65
+ private $sportsman;
65
66
66
67
// Getters
67
68
public function getName()
@@ -74,6 +75,12 @@ exists in your project::
74
75
return $this->age;
75
76
}
76
77
78
+ // Issers
79
+ public function isSportsman()
80
+ {
81
+ return $this->sportsman;
82
+ }
83
+
77
84
// Setters
78
85
public function setName($name)
79
86
{
@@ -84,6 +91,11 @@ exists in your project::
84
91
{
85
92
$this->age = $age;
86
93
}
94
+
95
+ public function setSportsman($sportsman)
96
+ {
97
+ $this->sportsman = $sportsman;
98
+ }
87
99
}
88
100
89
101
Now, if you want to serialize this object into JSON, you only need to
@@ -92,10 +104,11 @@ use the Serializer service created before::
92
104
$person = new Acme\Person();
93
105
$person->setName('foo');
94
106
$person->setAge(99);
107
+ $person->setSportsman(false);
95
108
96
109
$jsonContent = $serializer->serialize($person, 'json');
97
110
98
- // $jsonContent contains {"name":"foo","age":99}
111
+ // $jsonContent contains {"name":"foo","age":99,"sportsman":false }
99
112
100
113
echo $jsonContent; // or return it in a Response
101
114
@@ -124,7 +137,7 @@ method on the normalizer definition::
124
137
$encoder = new JsonEncoder();
125
138
126
139
$serializer = new Serializer(array($normalizer), array($encoder));
127
- $serializer->serialize($person, 'json'); // Output: {"name":"foo"}
140
+ $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false }
128
141
129
142
Deserializing an Object
130
143
-----------------------
@@ -136,6 +149,7 @@ of the ``Person`` class would be encoded in XML format::
136
149
<person>
137
150
<name>foo</name>
138
151
<age>99</age>
152
+ <sportsman>false</sportsman>
139
153
</person>
140
154
EOF;
141
155
@@ -181,6 +195,18 @@ method on the normalizer definition::
181
195
As a final result, the deserializer uses the ``first_name `` attribute as if
182
196
it were ``firstName `` and uses the ``getFirstName `` and ``setFirstName `` methods.
183
197
198
+ Serializing Boolean Attributes
199
+ ------------------------------
200
+
201
+ .. versionadded :: 2.5
202
+ Support for ``is* `` accessors in
203
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
204
+ was introduced in Symfony 2.5.
205
+
206
+ If you are using isser methods (methods prefixed by ``is ``, like
207
+ ``Acme\Person::isSportsman() ``), the Serializer component will automatically
208
+ detect and use it to serialize related attributes.
209
+
184
210
Using Callbacks to Serialize Properties with Object Instances
185
211
-------------------------------------------------------------
186
212
0 commit comments