From 5d75696f821d2012b752c330f3454492d2b54cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 4 Dec 2017 22:14:58 +0100 Subject: [PATCH 1/3] [Serializer] Add docs for attributes context key --- components/serializer.rst | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index f4e45bb2c36..49cae8fe6ac 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -333,6 +333,46 @@ You are now able to serialize only attributes in the groups you want:: .. _ignoring-attributes-when-serializing: +Selecting Specific Attributes +----------------------------- + +It is also possible to serialize only a set of specific attributes:: + + use Symfony\Component\Serializer\Serializer; + use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; + + class User + { + public $familyName; + public $givenName; + public $company; + } + + class Company + { + public $name; + public $address; + } + + $company = new Company(); + $company->name = 'Les-Tilleuls.coop'; + $company->address = 'Lille, France'; + + $user = new User(); + $user->familyName = 'Dunglas'; + $user->givenName = 'Kévin'; + $user->company = $company; + + $serializer = new Serializer(array(new ObjectNormalizer())); + + $data = $serializer->normalize($user, null, array('attributes' => array('familyName', 'company' => ['name']))); + // $data = array('familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']); + +Only attributes that are not ignored (see below) are available. +If some serialization groups are set, only attributes allowed by those groups can be used. + +As for groups, attributes can be applied during both the serialization and deserialization. + Ignoring Attributes ------------------- From ea211abab9d220f76d5bef68b43baf776365cd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 6 Dec 2017 10:03:07 +0100 Subject: [PATCH 2/3] Review --- components/serializer.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 49cae8fe6ac..a921e438186 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -366,12 +366,12 @@ It is also possible to serialize only a set of specific attributes:: $serializer = new Serializer(array(new ObjectNormalizer())); $data = $serializer->normalize($user, null, array('attributes' => array('familyName', 'company' => ['name']))); - // $data = array('familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']); + // $data = array('familyName' => 'Dunglas', 'company' => array('name' => 'Les-Tilleuls.coop')); Only attributes that are not ignored (see below) are available. If some serialization groups are set, only attributes allowed by those groups can be used. -As for groups, attributes can be applied during both the serialization and deserialization. +As for groups, attributes can be applied during both the serialization and deserialization process. Ignoring Attributes ------------------- From d48d6d4a5659d044810734f5f70a452e532060b3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 8 Dec 2017 17:18:30 +0100 Subject: [PATCH 3/3] Minor reword --- components/serializer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index a921e438186..2c37d040ec9 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -371,7 +371,7 @@ It is also possible to serialize only a set of specific attributes:: Only attributes that are not ignored (see below) are available. If some serialization groups are set, only attributes allowed by those groups can be used. -As for groups, attributes can be applied during both the serialization and deserialization process. +As for groups, attributes can be selected during both the serialization and deserialization process. Ignoring Attributes -------------------