@@ -33,6 +33,9 @@ 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
+ To use the ``ObjectNormalizer ``, the :doc: `PropertyAccess component </components/property_access/index >`
37
+ must also be installed.
38
+
36
39
Usage
37
40
-----
38
41
@@ -43,18 +46,18 @@ which Encoders and Normalizer are going to be available::
43
46
use Symfony\Component\Serializer\Serializer;
44
47
use Symfony\Component\Serializer\Encoder\XmlEncoder;
45
48
use Symfony\Component\Serializer\Encoder\JsonEncoder;
46
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
49
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
47
50
48
51
$encoders = array(new XmlEncoder(), new JsonEncoder());
49
- $normalizers = array(new GetSetMethodNormalizer ());
52
+ $normalizers = array(new ObjectNormalizer ());
50
53
51
54
$serializer = new Serializer($normalizers, $encoders);
52
55
53
- There are several normalizers available, e.g. the
54
- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ` or
55
- the :class: ` Symfony \\ Component \\ Serializer \\ Normalizer \\ PropertyNormalizer ` .
56
+ The preferred normalizer is
57
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer ` but other
58
+ normalizers are available .
56
59
To read more about them, refer to the `Normalizers `_ section of this page. All
57
- the examples shown below use the ``GetSetMethodNormalizer ``.
60
+ the examples shown below use the ``ObjectNormalizer ``.
58
61
59
62
Serializing an Object
60
63
---------------------
@@ -145,6 +148,28 @@ needs three parameters:
145
148
#. The name of the class this information will be decoded to
146
149
#. The encoder used to convert that information into an array
147
150
151
+ Deserializing in an Existing Object
152
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153
+
154
+ The serializer can also be used to update an existing object::
155
+
156
+ $person = new Acme\Person();
157
+ $person->setName('bar');
158
+ $person->setAge(99);
159
+ $person->setSportsman(true);
160
+
161
+ $data = <<<EOF
162
+ <person>
163
+ <name>foo</name>
164
+ <age>69</age>
165
+ </person>
166
+ EOF;
167
+
168
+ $serializer->deserialize($data, 'Acme\Person', 'xml', array('object_to_populate' => $person));
169
+ // $obj2 = Acme\Person(name: 'foo', age: '99', sportsman: true)
170
+
171
+ This is a common need when working with an ORM.
172
+
148
173
Attributes Groups
149
174
-----------------
150
175
@@ -283,8 +308,13 @@ You are now able to serialize only attributes in the groups you want::
283
308
Ignoring Attributes
284
309
-------------------
285
310
311
+ .. note ::
312
+
313
+ Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
314
+ method is considered best practice.
315
+
286
316
.. versionadded :: 2.3
287
- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
317
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
288
318
method was introduced in Symfony 2.3.
289
319
290
320
.. versionadded :: 2.7
@@ -293,14 +323,14 @@ Ignoring Attributes
293
323
294
324
As an option, there's a way to ignore attributes from the origin object. To remove
295
325
those attributes use the
296
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
326
+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
297
327
method on the normalizer definition::
298
328
299
329
use Symfony\Component\Serializer\Serializer;
300
330
use Symfony\Component\Serializer\Encoder\JsonEncoder;
301
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
331
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
302
332
303
- $normalizer = new GetSetMethodNormalizer ();
333
+ $normalizer = new ObjectNormalizer ();
304
334
$normalizer->setIgnoredAttributes(array('age'));
305
335
$encoder = new JsonEncoder();
306
336
@@ -357,11 +387,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
357
387
and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
358
388
359
389
use Symfony\Component\Serializer\Encoder\JsonEncoder
360
- use Symfony\Component\Serializer\Normalizer\PropertyNormalizer ;
390
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
361
391
use Symfony\Component\Serializer\Serializer;
362
392
363
393
$nameConverter = new OrgPrefixNameConverter();
364
- $normalizer = new PropertyNormalizer (null, $nameConverter);
394
+ $normalizer = new ObjectNormalizer (null, $nameConverter);
365
395
366
396
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
367
397
@@ -392,9 +422,9 @@ snake_case and CamelCased styles during serialization and deserialization
392
422
processes::
393
423
394
424
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
395
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
425
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
396
426
397
- $normalizer = new GetSetMethodNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
427
+ $normalizer = new ObjectNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
398
428
399
429
class Person
400
430
{
@@ -425,6 +455,9 @@ If you are using isser methods (methods prefixed by ``is``, like
425
455
``Acme\Person::isSportsman() ``), the Serializer component will automatically
426
456
detect and use it to serialize related attributes.
427
457
458
+ The ``ObjectNormalizer `` also take care of methods starting by ``has ``, ``add ``
459
+ and ``remove ``.
460
+
428
461
Using Callbacks to Serialize Properties with Object Instances
429
462
-------------------------------------------------------------
430
463
@@ -461,23 +494,42 @@ Normalizers
461
494
462
495
There are several types of normalizers available:
463
496
497
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
498
+ This normalizer leverages the :doc: `PropertyAccess Component </components/property_access/index >`
499
+ to read and write in the object. It means that it can access to properties
500
+ directly and trough getters, setters, hassers, adders and removers.
501
+ It supports calling the constructor during the denormalization process.
502
+
503
+ Objects are normalized to a map of property names (method name stripped of
504
+ the "get"/"set"/"has"/"remove" prefix and converted to lower case) to property
505
+ values.
506
+
507
+ The ``ObjectNormalizer `` is the most powerful normalizer. It is a configured
508
+ by default when using the Symfony Standard Edition with the serializer enabled.
509
+
464
510
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
465
511
This normalizer reads the content of the class by calling the "getters"
466
512
(public methods starting with "get"). It will denormalize data by calling
467
513
the constructor and the "setters" (public methods starting with "set").
468
514
469
- Objects are serialized to a map of property names (method name stripped of
515
+ Objects are normalized to a map of property names (method name stripped of
470
516
the "get" prefix and converted to lower case) to property values.
471
517
472
518
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
473
519
This normalizer directly reads and writes public properties as well as
474
- **private and protected ** properties. Objects are normalized to a map of
475
- property names to property values.
520
+ **private and protected ** properties.
521
+ It supports calling the constructor during the denormalization process.
522
+
523
+ Objects are normalized to a map of property names to property values.
476
524
477
525
.. versionadded :: 2.6 The
478
526
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
479
527
class was introduced in Symfony 2.6.
480
528
529
+ .. versionadded :: 2.7 The
530
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
531
+ class was introduced in Symfony 2.7.
532
+
481
533
Handling Circular References
482
534
----------------------------
483
535
@@ -563,7 +615,7 @@ by custom callables. This is especially useful when serializing entities
563
615
having unique identifiers::
564
616
565
617
$encoder = new JsonEncoder();
566
- $normalizer = new GetSetMethodNormalizer ();
618
+ $normalizer = new ObjectNormalizer ();
567
619
568
620
$normalizer->setCircularReferenceHandler(function ($object) {
569
621
return $object->getName();
0 commit comments