@@ -168,7 +168,7 @@ needs three parameters:
168
168
169
169
By default, additional attributes that are not mapped to the denormalized object
170
170
will be ignored by the Serializer component. If you prefer to throw an exception
171
- when this happens, set the ``allow_extra_attributes `` context option to
171
+ when this happens, set the ``AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES `` context option to
172
172
``false `` and provide an object that implements ``ClassMetadataFactoryInterface ``
173
173
when constructing the normalizer::
174
174
@@ -188,7 +188,7 @@ when constructing the normalizer::
188
188
// this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException
189
189
// because "city" is not an attribute of the Person class
190
190
$person = $serializer->deserialize($data, 'App\Model\Person', 'xml', [
191
- 'allow_extra_attributes' => false,
191
+ AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
192
192
]);
193
193
194
194
Deserializing in an Existing Object
@@ -209,12 +209,12 @@ The serializer can also be used to update an existing object::
209
209
</person>
210
210
EOF;
211
211
212
- $serializer->deserialize($data, Person::class, 'xml', ['object_to_populate' => $person]);
212
+ $serializer->deserialize($data, Person::class, 'xml', [AbstractNormalizer::OBJECT_TO_POPULATE => $person]);
213
213
// $person = App\Model\Person(name: 'foo', age: '69', sportsperson: true)
214
214
215
215
This is a common need when working with an ORM.
216
216
217
- The ``OBJECT_TO_POPULATE `` is only used for the top level object. If that object
217
+ The ``AbstractNormalizer:: OBJECT_TO_POPULATE `` is only used for the top level object. If that object
218
218
is the root of a tree structure, all child elements that exist in the
219
219
normalized data will be re-created with new instances.
220
220
@@ -377,6 +377,7 @@ Selecting Specific Attributes
377
377
378
378
It is also possible to serialize only a set of specific attributes::
379
379
380
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
380
381
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
381
382
use Symfony\Component\Serializer\Serializer;
382
383
@@ -404,7 +405,7 @@ It is also possible to serialize only a set of specific attributes::
404
405
405
406
$serializer = new Serializer([new ObjectNormalizer()]);
406
407
407
- $data = $serializer->normalize($user, null, ['attributes' => ['familyName', 'company' => ['name']]]);
408
+ $data = $serializer->normalize($user, null, [AbstractNormalizer::ATTRIBUTES => ['familyName', 'company' => ['name']]]);
408
409
// $data = ['familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']];
409
410
410
411
Only attributes that are not ignored (see below) are available.
@@ -416,11 +417,12 @@ Ignoring Attributes
416
417
-------------------
417
418
418
419
As an option, there's a way to ignore attributes from the origin object.
419
- To remove those attributes provide an array via the ``ignored_attributes ``
420
+ To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES ``
420
421
key in the ``context `` parameter of the desired serializer method::
421
422
422
423
use Acme\Person;
423
424
use Symfony\Component\Serializer\Encoder\JsonEncoder;
425
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
424
426
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
425
427
use Symfony\Component\Serializer\Serializer;
426
428
@@ -432,12 +434,12 @@ key in the ``context`` parameter of the desired serializer method::
432
434
$encoder = new JsonEncoder();
433
435
434
436
$serializer = new Serializer([$normalizer], [$encoder]);
435
- $serializer->serialize($person, 'json', ['ignored_attributes' => ['age']]); // Output: {"name":"foo"}
437
+ $serializer->serialize($person, 'json', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['age']]); // Output: {"name":"foo"}
436
438
437
439
.. deprecated :: 4.2
438
440
439
441
The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
440
- method that was used as an alternative to the ``ignored_attributes `` option
442
+ method that was used as an alternative to the ``AbstractNormalizer::IGNORED_ATTRIBUTES `` option
441
443
was deprecated in Symfony 4.2.
442
444
443
445
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
@@ -873,7 +875,7 @@ Skipping ``null`` Values
873
875
------------------------
874
876
875
877
By default, the Serializer will preserve properties containing a ``null `` value.
876
- You can change this behavior by setting the ``skip_null_values `` context option
878
+ You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NULL_VALUES `` context option
877
879
to ``true ``::
878
880
879
881
$dummy = new class {
@@ -882,7 +884,7 @@ to ``true``::
882
884
};
883
885
884
886
$normalizer = new ObjectNormalizer();
885
- $result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]);
887
+ $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
886
888
// ['bar' => 'notNull']
887
889
888
890
.. _component-serializer-handling-circular-references :
@@ -983,7 +985,7 @@ having unique identifiers::
983
985
.. deprecated :: 4.2
984
986
985
987
The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setCircularReferenceHandler `
986
- method is deprecated since Symfony 4.2. Use the ``circular_reference_handler ``
988
+ method is deprecated since Symfony 4.2. Use the ``AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER ``
987
989
key of the context instead.
988
990
989
991
Handling Serialization Depth
@@ -1063,11 +1065,11 @@ in a Symfony application. When using the standalone component, refer to
1063
1065
:ref: `the groups documentation <component-serializer-attributes-groups >` to
1064
1066
learn how to do that.
1065
1067
1066
- The check is only done if the ``enable_max_depth `` key of the serializer context
1068
+ The check is only done if the ``AbstractObjectNormalizer::ENABLE_MAX_DEPTH `` key of the serializer context
1067
1069
is set to ``true ``. In the following example, the third level is not serialized
1068
1070
because it is deeper than the configured maximum depth of 2::
1069
1071
1070
- $result = $serializer->normalize($level1, null, ['enable_max_depth' => true]);
1072
+ $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
1071
1073
/*
1072
1074
$result = [
1073
1075
'foo' => 'level1',
@@ -1088,6 +1090,7 @@ having unique identifiers::
1088
1090
use Symfony\Component\Serializer\Annotation\MaxDepth;
1089
1091
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1090
1092
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1093
+ use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
1091
1094
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1092
1095
use Symfony\Component\Serializer\Serializer;
1093
1096
@@ -1126,7 +1129,7 @@ having unique identifiers::
1126
1129
1127
1130
$serializer = new Serializer([$normalizer]);
1128
1131
1129
- $result = $serializer->normalize($level1, null, [ObjectNormalizer ::ENABLE_MAX_DEPTH => true]);
1132
+ $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer ::ENABLE_MAX_DEPTH => true]);
1130
1133
/*
1131
1134
$result = [
1132
1135
'id' => 1,
@@ -1262,6 +1265,7 @@ If the class constructor defines arguments, as usually happens with
1262
1265
arguments are missing. In those cases, use the ``default_constructor_arguments ``
1263
1266
context option::
1264
1267
1268
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1265
1269
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1266
1270
use Symfony\Component\Serializer\Serializer;
1267
1271
@@ -1283,7 +1287,7 @@ context option::
1283
1287
$data = $serializer->denormalize(
1284
1288
['foo' => 'Hello'],
1285
1289
'MyObj',
1286
- ['default_constructor_arguments' => [
1290
+ [AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [
1287
1291
'MyObj' => ['foo' => '', 'bar' => ''],
1288
1292
]]
1289
1293
);
0 commit comments