@@ -11,17 +11,14 @@ The Serializer Component
1111In order to do so, the Serializer component follows the following
1212simple schema.
1313
14- .. _component-serializer-encoders :
15- .. _component-serializer-normalizers :
16-
1714.. image :: /_images/components/serializer/serializer_workflow.png
1815
1916As you can see in the picture above, an array is used as a man in
2017the middle. This way, Encoders will only deal with turning specific
2118**formats ** into **arrays ** and vice versa. The same way, Normalizers
2219will deal with turning specific **objects ** into **arrays ** and vice versa.
2320
24- Serialization is a complex topic. This component may not cover all your use cases out of the box,
21+ Serialization is a complex topic. This component may not cover all your use cases out of the box,
2522but it can be useful for developing tools to serialize and deserialize your objects.
2623
2724Installation
@@ -333,6 +330,46 @@ You are now able to serialize only attributes in the groups you want::
333330
334331.. _ignoring-attributes-when-serializing :
335332
333+ Selecting Specific Attributes
334+ -----------------------------
335+
336+ It is also possible to serialize only a set of specific attributes::
337+
338+ use Symfony\Component\Serializer\Serializer;
339+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
340+
341+ class User
342+ {
343+ public $familyName;
344+ public $givenName;
345+ public $company;
346+ }
347+
348+ class Company
349+ {
350+ public $name;
351+ public $address;
352+ }
353+
354+ $company = new Company();
355+ $company->name = 'Les-Tilleuls.coop';
356+ $company->address = 'Lille, France';
357+
358+ $user = new User();
359+ $user->familyName = 'Dunglas';
360+ $user->givenName = 'Kévin';
361+ $user->company = $company;
362+
363+ $serializer = new Serializer(array(new ObjectNormalizer()));
364+
365+ $data = $serializer->normalize($user, null, array('attributes' => array('familyName', 'company' => ['name'])));
366+ // $data = array('familyName' => 'Dunglas', 'company' => array('name' => 'Les-Tilleuls.coop'));
367+
368+ Only attributes that are not ignored (see below) are available.
369+ If some serialization groups are set, only attributes allowed by those groups can be used.
370+
371+ As for groups, attributes can be selected during both the serialization and deserialization process.
372+
336373Ignoring Attributes
337374-------------------
338375
@@ -503,6 +540,8 @@ When serializing, you can set a callback to format a specific object property::
503540 $serializer->serialize($person, 'json');
504541 // Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
505542
543+ .. _component-serializer-normalizers :
544+
506545Normalizers
507546-----------
508547
@@ -562,6 +601,8 @@ There are several types of normalizers available:
562601 This normalizer converts :phpclass: `SplFileInfo ` objects into a data URI
563602 string (``data:... ``) such that files can be embedded into serialized data.
564603
604+ .. _component-serializer-encoders :
605+
565606Encoders
566607--------
567608
0 commit comments