Skip to content

Commit f913dd7

Browse files
committed
feature #3603 [Serializer] Support for is.* getters in GetSetMethodNormalizer (tiraeth)
This PR was submitted for the master branch but it was merged into the 2.5 branch instead (closes #3603). Discussion ---------- [Serializer] Support for is.* getters in GetSetMethodNormalizer | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#10314) | Applies to | 2.5+ | Fixed tickets | N/A Commits ------- fcfcfb9 [Serializer] added a note about is* getters support with GetSetMethodNormalizer
2 parents 704d206 + 1f5d980 commit f913dd7

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

components/serializer.rst

Lines changed: 28 additions & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy