Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Serializer] Support for is.* getters in GetSetMethodNormalizer#3603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
tiraeth wants to merge1 commit intosymfony:masterfromtiraeth:master
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletionscomponents/serializer.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -62,6 +62,7 @@ exists in your project::
{
private $age;
private $name;
private $sportsman;

// Getters
public function getName()
Expand All@@ -74,6 +75,12 @@ exists in your project::
return $this->age;
}

// Issers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's a detail, but as there is only one isser, couldn't it be singular?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Details are very important :)

But imo, we are talking about issers in general here and show an example.

public function isSportsman()
{
return $this->sportsman;
}

// Setters
public function setName($name)
{
Expand All@@ -84,6 +91,11 @@ exists in your project::
{
$this->age = $age;
}

public function setSportsman($sportsman)
{
$this->sportsman = $sportsman;
}
}

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

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

// $jsonContent contains {"name":"foo","age":99}
// $jsonContent contains {"name":"foo","age":99,"sportsman":false}

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

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

$serializer = new Serializer(array($normalizer), array($encoder));
$serializer->serialize($person, 'json'); // Output: {"name":"foo"}
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}

Deserializing an Object
-----------------------
Expand All@@ -136,6 +149,7 @@ of the ``Person`` class would be encoded in XML format::
<person>
<name>foo</name>
<age>99</age>
<sportsman>false</sportsman>
</person>
EOF;

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

Serializing Boolean Attributes
------------------------------

.. versionadded:: 2.5
Support for ``is*`` accessors in
:class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
was introduced in Symfony 2.5.

If you are using isser methods (methods prefixed by ``is``, like
``Acme\Person::isSportsman()``), the Serializer component will automatically
detect and use it to serialize related attributes.

Using Callbacks to Serialize Properties with Object Instances
-------------------------------------------------------------

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp