diff --git a/components/serializer.rst b/components/serializer.rst index 94c1ca0d5b1..cb57ee44d81 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -168,7 +168,7 @@ needs three parameters: By default, additional attributes that are not mapped to the denormalized object will be ignored by the Serializer component. If you prefer to throw an exception -when this happens, set the ``allow_extra_attributes`` context option to +when this happens, set the ``AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES`` context option to ``false`` and provide an object that implements ``ClassMetadataFactoryInterface`` when constructing the normalizer:: @@ -188,7 +188,7 @@ when constructing the normalizer:: // this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException // because "city" is not an attribute of the Person class $person = $serializer->deserialize($data, 'App\Model\Person', 'xml', [ - 'allow_extra_attributes' => false, + AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false, ]); Deserializing in an Existing Object @@ -209,12 +209,12 @@ The serializer can also be used to update an existing object:: EOF; - $serializer->deserialize($data, Person::class, 'xml', ['object_to_populate' => $person]); + $serializer->deserialize($data, Person::class, 'xml', [AbstractNormalizer::OBJECT_TO_POPULATE => $person]); // $person = App\Model\Person(name: 'foo', age: '69', sportsperson: true) This is a common need when working with an ORM. -The ``OBJECT_TO_POPULATE`` is only used for the top level object. If that object +The ``AbstractNormalizer::OBJECT_TO_POPULATE`` is only used for the top level object. If that object is the root of a tree structure, all child elements that exist in the normalized data will be re-created with new instances. @@ -377,6 +377,7 @@ Selecting Specific Attributes It is also possible to serialize only a set of specific attributes:: + use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; @@ -404,7 +405,7 @@ It is also possible to serialize only a set of specific attributes:: $serializer = new Serializer([new ObjectNormalizer()]); - $data = $serializer->normalize($user, null, ['attributes' => ['familyName', 'company' => ['name']]]); + $data = $serializer->normalize($user, null, [AbstractNormalizer::ATTRIBUTES => ['familyName', 'company' => ['name']]]); // $data = ['familyName' => 'Dunglas', 'company' => ['name' => 'Les-Tilleuls.coop']]; Only attributes that are not ignored (see below) are available. @@ -416,11 +417,12 @@ Ignoring Attributes ------------------- As an option, there's a way to ignore attributes from the origin object. -To remove those attributes provide an array via the ``ignored_attributes`` +To remove those attributes provide an array via the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` key in the ``context`` parameter of the desired serializer method:: use Acme\Person; use Symfony\Component\Serializer\Encoder\JsonEncoder; + use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; @@ -432,12 +434,12 @@ key in the ``context`` parameter of the desired serializer method:: $encoder = new JsonEncoder(); $serializer = new Serializer([$normalizer], [$encoder]); - $serializer->serialize($person, 'json', ['ignored_attributes' => ['age']]); // Output: {"name":"foo"} + $serializer->serialize($person, 'json', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['age']]); // Output: {"name":"foo"} .. deprecated:: 4.2 The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes` - method that was used as an alternative to the ``ignored_attributes`` option + method that was used as an alternative to the ``AbstractNormalizer::IGNORED_ATTRIBUTES`` option was deprecated in Symfony 4.2. .. _component-serializer-converting-property-names-when-serializing-and-deserializing: @@ -873,7 +875,7 @@ Skipping ``null`` Values ------------------------ By default, the Serializer will preserve properties containing a ``null`` value. -You can change this behavior by setting the ``skip_null_values`` context option +You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NULL_VALUES`` context option to ``true``:: $dummy = new class { @@ -882,7 +884,7 @@ to ``true``:: }; $normalizer = new ObjectNormalizer(); - $result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]); + $result = $normalizer->normalize($dummy, 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]); // ['bar' => 'notNull'] .. _component-serializer-handling-circular-references: @@ -983,7 +985,7 @@ having unique identifiers:: .. deprecated:: 4.2 The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler` - method is deprecated since Symfony 4.2. Use the ``circular_reference_handler`` + method is deprecated since Symfony 4.2. Use the ``AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER`` key of the context instead. Handling Serialization Depth @@ -1063,11 +1065,11 @@ in a Symfony application. When using the standalone component, refer to :ref:`the groups documentation ` to learn how to do that. -The check is only done if the ``enable_max_depth`` key of the serializer context +The check is only done if the ``AbstractObjectNormalizer::ENABLE_MAX_DEPTH`` key of the serializer context is set to ``true``. In the following example, the third level is not serialized because it is deeper than the configured maximum depth of 2:: - $result = $serializer->normalize($level1, null, ['enable_max_depth' => true]); + $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]); /* $result = [ 'foo' => 'level1', @@ -1088,6 +1090,7 @@ having unique identifiers:: use Symfony\Component\Serializer\Annotation\MaxDepth; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; + use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; @@ -1126,7 +1129,7 @@ having unique identifiers:: $serializer = new Serializer([$normalizer]); - $result = $serializer->normalize($level1, null, [ObjectNormalizer::ENABLE_MAX_DEPTH => true]); + $result = $serializer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]); /* $result = [ 'id' => 1, @@ -1262,6 +1265,7 @@ If the class constructor defines arguments, as usually happens with arguments are missing. In those cases, use the ``default_constructor_arguments`` context option:: + use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; @@ -1283,7 +1287,7 @@ context option:: $data = $serializer->denormalize( ['foo' => 'Hello'], 'MyObj', - ['default_constructor_arguments' => [ + [AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [ 'MyObj' => ['foo' => '', 'bar' => ''], ]] );