Skip to content

Commit 1f5d980

Browse files
Marcin Chwedziakweaverryan
Marcin Chwedziak
authored andcommitted
added a note about is* getters support with GetSetMethodNormalizer
1 parent 704d206 commit 1f5d980

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

components/serializer.rst

+28-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ exists in your project::
6262
{
6363
private $age;
6464
private $name;
65+
private $sportsman;
6566

6667
// Getters
6768
public function getName()
@@ -74,6 +75,12 @@ exists in your project::
7475
return $this->age;
7576
}
7677

78+
// Issers
79+
public function isSportsman()
80+
{
81+
return $this->sportsman;
82+
}
83+
7784
// Setters
7885
public function setName($name)
7986
{
@@ -84,6 +91,11 @@ exists in your project::
8491
{
8592
$this->age = $age;
8693
}
94+
95+
public function setSportsman($sportsman)
96+
{
97+
$this->sportsman = $sportsman;
98+
}
8799
}
88100

89101
Now, if you want to serialize this object into JSON, you only need to
@@ -92,10 +104,11 @@ use the Serializer service created before::
92104
$person = new Acme\Person();
93105
$person->setName('foo');
94106
$person->setAge(99);
107+
$person->setSportsman(false);
95108

96109
$jsonContent = $serializer->serialize($person, 'json');
97110

98-
// $jsonContent contains {"name":"foo","age":99}
111+
// $jsonContent contains {"name":"foo","age":99,"sportsman":false}
99112

100113
echo $jsonContent; // or return it in a Response
101114

@@ -124,7 +137,7 @@ method on the normalizer definition::
124137
$encoder = new JsonEncoder();
125138

126139
$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}
128141

129142
Deserializing an Object
130143
-----------------------
@@ -136,6 +149,7 @@ of the ``Person`` class would be encoded in XML format::
136149
<person>
137150
<name>foo</name>
138151
<age>99</age>
152+
<sportsman>false</sportsman>
139153
</person>
140154
EOF;
141155

@@ -181,6 +195,18 @@ method on the normalizer definition::
181195
As a final result, the deserializer uses the ``first_name`` attribute as if
182196
it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods.
183197

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+
184210
Using Callbacks to Serialize Properties with Object Instances
185211
-------------------------------------------------------------
186212

0 commit comments

Comments
 (0)