@@ -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
@@ -372,6 +372,7 @@ Selecting Specific Attributes
372
372
373
373
It is also possible to serialize only a set of specific attributes::
374
374
375
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
375
376
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
376
377
use Symfony\Component\Serializer\Serializer;
377
378
@@ -399,7 +400,7 @@ It is also possible to serialize only a set of specific attributes::
399
400
400
401
$serializer = new Serializer([new ObjectNormalizer()]);
401
402
402
- $data = $serializer->normalize($user, null, ['attributes' => ['familyName', 'company' => ['name']]]);
403
+ $data = $serializer->normalize($user, null, [AbstractNormalizer::ATTRIBUTES => ['familyName', 'company' => ['name']]]);
403
404
// $data = ['familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']];
404
405
405
406
Only attributes that are not ignored (see below) are available.
@@ -411,11 +412,12 @@ Ignoring Attributes
411
412
-------------------
412
413
413
414
As an option, there's a way to ignore attributes from the origin object.
414
- To remove those attributes provide an array via the ``ignored_attributes ``
415
+ To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES ``
415
416
key in the ``context `` parameter of the desired serializer method::
416
417
417
418
use Acme\Person;
418
419
use Symfony\Component\Serializer\Encoder\JsonEncoder;
420
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
419
421
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
420
422
use Symfony\Component\Serializer\Serializer;
421
423
@@ -427,7 +429,7 @@ key in the ``context`` parameter of the desired serializer method::
427
429
$encoder = new JsonEncoder();
428
430
429
431
$serializer = new Serializer([$normalizer], [$encoder]);
430
- $serializer->serialize($person, 'json', ['ignored_attributes' => ['age']]); // Output: {"name":"foo"}
432
+ $serializer->serialize($person, 'json', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['age']]); // Output: {"name":"foo"}
431
433
432
434
.. _component-serializer-converting-property-names-when-serializing-and-deserializing :
433
435
@@ -843,7 +845,7 @@ Skipping ``null`` Values
843
845
------------------------
844
846
845
847
By default, the Serializer will preserve properties containing a ``null `` value.
846
- You can change this behavior by setting the ``skip_null_values `` context option
848
+ You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NULL_VALUES `` context option
847
849
to ``true ``::
848
850
849
851
$dummy = new class {
@@ -852,7 +854,7 @@ to ``true``::
852
854
};
853
855
854
856
$normalizer = new ObjectNormalizer();
855
- $result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]);
857
+ $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
856
858
// ['bar' => 'notNull']
857
859
858
860
.. _component-serializer-handling-circular-references :
@@ -1027,11 +1029,11 @@ in a Symfony application. When using the standalone component, refer to
1027
1029
:ref: `the groups documentation <component-serializer-attributes-groups >` to
1028
1030
learn how to do that.
1029
1031
1030
- The check is only done if the ``enable_max_depth `` key of the serializer context
1032
+ The check is only done if the ``AbstractObjectNormalizer::ENABLE_MAX_DEPTH `` key of the serializer context
1031
1033
is set to ``true ``. In the following example, the third level is not serialized
1032
1034
because it is deeper than the configured maximum depth of 2::
1033
1035
1034
- $result = $serializer->normalize($level1, null, ['enable_max_depth' => true]);
1036
+ $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
1035
1037
/*
1036
1038
$result = [
1037
1039
'foo' => 'level1',
@@ -1052,6 +1054,7 @@ having unique identifiers::
1052
1054
use Symfony\Component\Serializer\Annotation\MaxDepth;
1053
1055
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1054
1056
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1057
+ use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
1055
1058
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1056
1059
use Symfony\Component\Serializer\Serializer;
1057
1060
@@ -1090,7 +1093,7 @@ having unique identifiers::
1090
1093
1091
1094
$serializer = new Serializer([$normalizer]);
1092
1095
1093
- $result = $serializer->normalize($level1, null, [ObjectNormalizer ::ENABLE_MAX_DEPTH => true]);
1096
+ $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer ::ENABLE_MAX_DEPTH => true]);
1094
1097
/*
1095
1098
$result = [
1096
1099
'id' => 1,
@@ -1220,6 +1223,7 @@ If the class constructor defines arguments, as usually happens with
1220
1223
arguments are missing. In those cases, use the ``default_constructor_arguments ``
1221
1224
context option::
1222
1225
1226
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1223
1227
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1224
1228
use Symfony\Component\Serializer\Serializer;
1225
1229
@@ -1241,7 +1245,7 @@ context option::
1241
1245
$data = $serializer->denormalize(
1242
1246
['foo' => 'Hello'],
1243
1247
'MyObj',
1244
- ['default_constructor_arguments' => [
1248
+ [AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [
1245
1249
'MyObj' => ['foo' => '', 'bar' => ''],
1246
1250
]]
1247
1251
);
0 commit comments