Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4400f3

Browse files
committed
[Serializer] documentation for allow_extra_attributes
1 parent b386600 commit f4400f3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

components/serializer.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ needs three parameters:
151151
#. The name of the class this information will be decoded to
152152
#. The encoder used to convert that information into an array
153153

154+
Extra attributes, attributes which are not mapped to the object, are allowed per default
155+
by the serializer.
156+
:method:`Symfony\\Component\\Serializer\\Serializer::deserialize`
157+
has a fourth parameter, the context. If you want to throw an exception if an attribute is outside
158+
the object scope, use ``allow_extra_attributes`` as ``false``.
159+
160+
This will throws a :class:`Symfony\Component\Serializer\Exception\ExtraAttributesException` exception,
161+
because city is not an attribute of ``Acme\Person``::
162+
163+
$data = <<<EOF
164+
<person>
165+
<name>foo</name>
166+
<age>99</age>
167+
<city>Paris</city>
168+
</person>
169+
EOF;
170+
171+
$person = $serializer->deserialize($data, 'Acme\Person', 'xml', array(
172+
'allow_extra_attributes' => false,
173+
));
174+
175+
154176
Deserializing in an Existing Object
155177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156178

0 commit comments

Comments
 (0)