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

Use gender-neutral language in the main Serializer article#9795

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
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
40 changes: 20 additions & 20 deletionscomponents/serializer.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,9 +16,9 @@ simple schema.

.. image:: /_images/components/serializer/serializer_workflow.png

As you can see in the picture above, an array is used asa man in
the middle. This way,Encoders will only deal with turning specific
**formats** into **arrays** and vice versa. The same way, Normalizers
As you can see in the picture above, an array is used asan intermediary between
objects and serialized contents. This way,encoders will only deal with turning
specific**formats** into **arrays** and vice versa. The same way, Normalizers
will deal with turning specific **objects** into **arrays** and vice versa.

Serialization is a complex topic. This component may not cover all your use cases out of the box,
Expand DownExpand Up@@ -66,13 +66,13 @@ Serializing an Object
For the sake of this example, assume the following class already
exists in your project::

namespaceAcme;
namespaceApp\Model;

class Person
{
private $age;
private $name;
private $sportsman;
private $sportsperson;
private $createdAt;

// Getters
Expand All@@ -92,9 +92,9 @@ exists in your project::
}

// Issers
public functionisSportsman()
public functionisSportsperson()
{
return $this->sportsman;
return $this->sportsperson;
}

// Setters
Expand All@@ -108,9 +108,9 @@ exists in your project::
$this->age = $age;
}

public functionsetSportsman($sportsman)
public functionsetSportsperson($sportsperson)
{
$this->sportsman = $sportsman;
$this->sportsperson = $sportsperson;
}

public function setCreatedAt($createdAt)
Expand All@@ -122,14 +122,14 @@ exists in your project::
Now, if you want to serialize this object into JSON, you only need to
use the Serializer service created before::

$person = newAcme\Person();
$person = newApp\Model\Person();
$person->setName('foo');
$person->setAge(99);
$person->setSportsman(false);
$person->setSportsperson(false);

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

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

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

Expand All@@ -143,13 +143,13 @@ Deserializing an Object
You'll now learn how to do the exact opposite. This time, the information
of the ``Person`` class would be encoded in XML format::

useAcme\Person;
useApp\Model\Person;

$data = <<<EOF
<person>
<name>foo</name>
<age>99</age>
<sportsman>false</sportsman>
<sportsperson>false</sportsperson>
</person>
EOF;

Expand All@@ -171,7 +171,7 @@ The serializer can also be used to update an existing object::
$person = new Person();
$person->setName('bar');
$person->setAge(99);
$person->setSportsman(true);
$person->setSportsperson(true);

$data = <<<EOF
<person>
Expand All@@ -181,7 +181,7 @@ The serializer can also be used to update an existing object::
EOF;

$serializer->deserialize($data, Person::class, 'xml', array('object_to_populate' => $person));
// $person =Acme\Person(name: 'foo', age: '69',sportsman: true)
// $person =App\Model\Person(name: 'foo', age: '69',sportsperson: true)

This is a common need when working with an ORM.

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

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

Converting Property Names when Serializing and Deserializing
------------------------------------------------------------
Expand DownExpand Up@@ -474,8 +474,8 @@ Serializing Boolean Attributes
------------------------------

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.
``App\Model\Person::isSportsperson()``), the Serializer component will
automaticallydetect and use it to serialize related attributes.

The ``ObjectNormalizer`` also takes care of methods starting with ``has``, ``add``
and ``remove``.
Expand All@@ -485,7 +485,7 @@ Using Callbacks to Serialize Properties with Object Instances

When serializing, you can set a callback to format a specific object property::

useAcme\Person;
useApp\Model\Person;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp