@@ -33,8 +33,12 @@ You can install the component in 2 different ways:
33
33
* :doc: `Install it via Composer </components/using_components >` (``symfony/serializer `` on `Packagist `_);
34
34
* Use the official Git repository (https://github.com./symfony/Serializer).
35
35
36
+
36
37
.. include :: /components/require_autoload.rst.inc
37
38
39
+ To use the ``ObjectNormalizer ``, the :doc: `PropertyAccess component </components/property_access/index >`
40
+ must also be installed.
41
+
38
42
Usage
39
43
-----
40
44
@@ -45,18 +49,18 @@ which Encoders and Normalizer are going to be available::
45
49
use Symfony\Component\Serializer\Serializer;
46
50
use Symfony\Component\Serializer\Encoder\XmlEncoder;
47
51
use Symfony\Component\Serializer\Encoder\JsonEncoder;
48
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
52
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
49
53
50
54
$encoders = array(new XmlEncoder(), new JsonEncoder());
51
- $normalizers = array(new GetSetMethodNormalizer ());
55
+ $normalizers = array(new ObjectNormalizer ());
52
56
53
57
$serializer = new Serializer($normalizers, $encoders);
54
58
55
- There are several normalizers available, e.g. the
56
- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ` or
57
- the :class: ` Symfony \\ Component \\ Serializer \\ Normalizer \\ PropertyNormalizer ` .
59
+ The preferred normalizer is the
60
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `, but other
61
+ normalizers are available .
58
62
To read more about them, refer to the `Normalizers `_ section of this page. All
59
- the examples shown below use the ``GetSetMethodNormalizer ``.
63
+ the examples shown below use the ``ObjectNormalizer ``.
60
64
61
65
Serializing an Object
62
66
---------------------
@@ -147,6 +151,30 @@ needs three parameters:
147
151
#. The name of the class this information will be decoded to
148
152
#. The encoder used to convert that information into an array
149
153
154
+ Deserializing in an Existing Object
155
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156
+
157
+ The serializer can also be used to update an existing object::
158
+
159
+ $person = new Acme\Person();
160
+ $person->setName('bar');
161
+ $person->setAge(99);
162
+ $person->setSportsman(true);
163
+
164
+ $data = <<<EOF
165
+ <person>
166
+ <name>foo</name>
167
+ <age>69</age>
168
+ </person>
169
+ EOF;
170
+
171
+ $serializer->deserialize($data, 'Acme\Person', 'xml', array('object_to_populate' => $person));
172
+ // $obj2 = Acme\Person(name: 'foo', age: '99', sportsman: true)
173
+
174
+ This is a common need when working with an ORM.
175
+
176
+ .. _component-serializer-attributes-groups :
177
+
150
178
Attributes Groups
151
179
-----------------
152
180
@@ -200,6 +228,8 @@ like the following::
200
228
// For YAML
201
229
// $classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader('/path/to/your/definition.yml'));
202
230
231
+ .. _component-serializer-attributes-groups-annotations :
232
+
203
233
Then, create your groups definition:
204
234
205
235
.. configuration-block ::
@@ -285,8 +315,13 @@ You are now able to serialize only attributes in the groups you want::
285
315
Ignoring Attributes
286
316
-------------------
287
317
318
+ .. note ::
319
+
320
+ Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
321
+ method is considered best practice.
322
+
288
323
.. versionadded :: 2.3
289
- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
324
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
290
325
method was introduced in Symfony 2.3.
291
326
292
327
.. versionadded :: 2.7
@@ -295,14 +330,14 @@ Ignoring Attributes
295
330
296
331
As an option, there's a way to ignore attributes from the origin object. To remove
297
332
those attributes use the
298
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
333
+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
299
334
method on the normalizer definition::
300
335
301
336
use Symfony\Component\Serializer\Serializer;
302
337
use Symfony\Component\Serializer\Encoder\JsonEncoder;
303
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
338
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
304
339
305
- $normalizer = new GetSetMethodNormalizer ();
340
+ $normalizer = new ObjectNormalizer ();
306
341
$normalizer->setIgnoredAttributes(array('age'));
307
342
$encoder = new JsonEncoder();
308
343
@@ -359,11 +394,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
359
394
and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
360
395
361
396
use Symfony\Component\Serializer\Encoder\JsonEncoder
362
- use Symfony\Component\Serializer\Normalizer\PropertyNormalizer ;
397
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
363
398
use Symfony\Component\Serializer\Serializer;
364
399
365
400
$nameConverter = new OrgPrefixNameConverter();
366
- $normalizer = new PropertyNormalizer (null, $nameConverter);
401
+ $normalizer = new ObjectNormalizer (null, $nameConverter);
367
402
368
403
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
369
404
@@ -394,9 +429,9 @@ snake_case and CamelCased styles during serialization and deserialization
394
429
processes::
395
430
396
431
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
397
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
432
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
398
433
399
- $normalizer = new GetSetMethodNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
434
+ $normalizer = new ObjectNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
400
435
401
436
class Person
402
437
{
@@ -427,6 +462,9 @@ If you are using isser methods (methods prefixed by ``is``, like
427
462
``Acme\Person::isSportsman() ``), the Serializer component will automatically
428
463
detect and use it to serialize related attributes.
429
464
465
+ The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
466
+ and ``remove ``.
467
+
430
468
Using Callbacks to Serialize Properties with Object Instances
431
469
-------------------------------------------------------------
432
470
@@ -463,23 +501,42 @@ Normalizers
463
501
464
502
There are several types of normalizers available:
465
503
504
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
505
+ This normalizer leverages the :doc: `PropertyAccess Component </components/property_access/index >`
506
+ to read and write in the object. It means that it can access to properties
507
+ directly and through getters, setters, hassers, adders and removers. It supports
508
+ calling the constructor during the denormalization process.
509
+
510
+ Objects are normalized to a map of property names (method name stripped of
511
+ the "get"/"set"/"has"/"remove" prefix and converted to lower case) to property
512
+ values.
513
+
514
+ The ``ObjectNormalizer `` is the most powerful normalizer. It is a configured
515
+ by default when using the Symfony Standard Edition with the serializer enabled.
516
+
466
517
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
467
518
This normalizer reads the content of the class by calling the "getters"
468
519
(public methods starting with "get"). It will denormalize data by calling
469
520
the constructor and the "setters" (public methods starting with "set").
470
521
471
- Objects are serialized to a map of property names (method name stripped of
522
+ Objects are normalized to a map of property names (method name stripped of
472
523
the "get" prefix and converted to lower case) to property values.
473
524
474
525
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
475
526
This normalizer directly reads and writes public properties as well as
476
- **private and protected ** properties. Objects are normalized to a map of
477
- property names to property values.
527
+ **private and protected ** properties. It supports calling the constructor
528
+ during the denormalization process.
529
+
530
+ Objects are normalized to a map of property names to property values.
478
531
479
- .. versionadded :: 2.6 The
480
- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
532
+ .. versionadded :: 2.6
533
+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
481
534
class was introduced in Symfony 2.6.
482
535
536
+ .. versionadded :: 2.7
537
+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
538
+ class was introduced in Symfony 2.7.
539
+
483
540
Handling Circular References
484
541
----------------------------
485
542
@@ -565,7 +622,7 @@ by custom callables. This is especially useful when serializing entities
565
622
having unique identifiers::
566
623
567
624
$encoder = new JsonEncoder();
568
- $normalizer = new GetSetMethodNormalizer ();
625
+ $normalizer = new ObjectNormalizer ();
569
626
570
627
$normalizer->setCircularReferenceHandler(function ($object) {
571
628
return $object->getName();
0 commit comments