@@ -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,28 @@ 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
+
150
176
.. _component-serializer-attributes-groups :
151
177
152
178
Attributes Groups
@@ -289,8 +315,13 @@ You are now able to serialize only attributes in the groups you want::
289
315
Ignoring Attributes
290
316
-------------------
291
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
+
292
323
.. versionadded :: 2.3
293
- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
324
+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
294
325
method was introduced in Symfony 2.3.
295
326
296
327
.. versionadded :: 2.7
@@ -299,14 +330,14 @@ Ignoring Attributes
299
330
300
331
As an option, there's a way to ignore attributes from the origin object. To remove
301
332
those attributes use the
302
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
333
+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
303
334
method on the normalizer definition::
304
335
305
336
use Symfony\Component\Serializer\Serializer;
306
337
use Symfony\Component\Serializer\Encoder\JsonEncoder;
307
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
338
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
308
339
309
- $normalizer = new GetSetMethodNormalizer ();
340
+ $normalizer = new ObjectNormalizer ();
310
341
$normalizer->setIgnoredAttributes(array('age'));
311
342
$encoder = new JsonEncoder();
312
343
@@ -363,11 +394,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
363
394
and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
364
395
365
396
use Symfony\Component\Serializer\Encoder\JsonEncoder
366
- use Symfony\Component\Serializer\Normalizer\PropertyNormalizer ;
397
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
367
398
use Symfony\Component\Serializer\Serializer;
368
399
369
400
$nameConverter = new OrgPrefixNameConverter();
370
- $normalizer = new PropertyNormalizer (null, $nameConverter);
401
+ $normalizer = new ObjectNormalizer (null, $nameConverter);
371
402
372
403
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
373
404
@@ -398,9 +429,9 @@ snake_case and CamelCased styles during serialization and deserialization
398
429
processes::
399
430
400
431
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
401
- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
432
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
402
433
403
- $normalizer = new GetSetMethodNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
434
+ $normalizer = new ObjectNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
404
435
405
436
class Person
406
437
{
@@ -431,6 +462,9 @@ If you are using isser methods (methods prefixed by ``is``, like
431
462
``Acme\Person::isSportsman() ``), the Serializer component will automatically
432
463
detect and use it to serialize related attributes.
433
464
465
+ The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
466
+ and ``remove ``.
467
+
434
468
Using Callbacks to Serialize Properties with Object Instances
435
469
-------------------------------------------------------------
436
470
@@ -467,23 +501,42 @@ Normalizers
467
501
468
502
There are several types of normalizers available:
469
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 trough 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
+
470
517
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
471
518
This normalizer reads the content of the class by calling the "getters"
472
519
(public methods starting with "get"). It will denormalize data by calling
473
520
the constructor and the "setters" (public methods starting with "set").
474
521
475
- 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
476
523
the "get" prefix and converted to lower case) to property values.
477
524
478
525
:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
479
526
This normalizer directly reads and writes public properties as well as
480
- **private and protected ** properties. Objects are normalized to a map of
481
- 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.
482
531
483
- .. versionadded :: 2.6 The
484
- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
532
+ .. versionadded :: 2.6
533
+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
485
534
class was introduced in Symfony 2.6.
486
535
536
+ .. versionadded :: 2.7
537
+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
538
+ class was introduced in Symfony 2.7.
539
+
487
540
Handling Circular References
488
541
----------------------------
489
542
@@ -569,7 +622,7 @@ by custom callables. This is especially useful when serializing entities
569
622
having unique identifiers::
570
623
571
624
$encoder = new JsonEncoder();
572
- $normalizer = new GetSetMethodNormalizer ();
625
+ $normalizer = new ObjectNormalizer ();
573
626
574
627
$normalizer->setCircularReferenceHandler(function ($object) {
575
628
return $object->getName();
0 commit comments