@@ -11,17 +11,14 @@ The Serializer Component
11
11
In order to do so, the Serializer component follows the following
12
12
simple schema.
13
13
14
- .. _component-serializer-encoders :
15
- .. _component-serializer-normalizers :
16
-
17
14
.. image :: /_images/components/serializer/serializer_workflow.png
18
15
19
16
As you can see in the picture above, an array is used as a man in
20
17
the middle. This way, Encoders will only deal with turning specific
21
18
**formats ** into **arrays ** and vice versa. The same way, Normalizers
22
19
will deal with turning specific **objects ** into **arrays ** and vice versa.
23
20
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,
25
22
but it can be useful for developing tools to serialize and deserialize your objects.
26
23
27
24
Installation
@@ -333,6 +330,46 @@ You are now able to serialize only attributes in the groups you want::
333
330
334
331
.. _ignoring-attributes-when-serializing :
335
332
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
+
336
373
Ignoring Attributes
337
374
-------------------
338
375
@@ -503,6 +540,8 @@ When serializing, you can set a callback to format a specific object property::
503
540
$serializer->serialize($person, 'json');
504
541
// Output: {"name":"cordoval", "age": 34, "createdAt": "2014-03-22T09:43:12-0500"}
505
542
543
+ .. _component-serializer-normalizers :
544
+
506
545
Normalizers
507
546
-----------
508
547
@@ -562,6 +601,8 @@ There are several types of normalizers available:
562
601
This normalizer converts :phpclass: `SplFileInfo ` objects into a data URI
563
602
string (``data:... ``) such that files can be embedded into serialized data.
564
603
604
+ .. _component-serializer-encoders :
605
+
565
606
Encoders
566
607
--------
567
608
0 commit comments