Skip to content

Add XML encoder comment tip and docs #10453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`::

.. note::

You can also implement
You can also implement
:class:`Symfony\\Component\\Serializer\\NameConverter\\AdvancedNameConverterInterface`
to access to the current class name, format and context.

Expand Down Expand Up @@ -722,14 +722,16 @@ The ``XmlEncoder`` will encode this object like that::
<bar>1</bar>
</response>

Be aware that this encoder will consider keys beginning with ``@`` as attributes::
Be aware that this encoder will consider keys beginning with ``@`` as attributes, and will use
the key ``#comment`` for encoding XML comments::

$encoder = new XmlEncoder();
$encoder->encode(array('foo' => array('@bar' => 'value')));
$encoder->encode(array('foo' => array('@bar' => 'value'), 'qux' => array('#comment' => 'A comment));
// will return:
// <?xml version="1.0"?>
// <response>
// <foo bar="value" />
// <qux><!-- A comment --!><qux>
// </response>

You can pass the context key ``as_collection`` in order to have the results
Expand All @@ -741,9 +743,13 @@ always as a collection.
.. tip::

XML comments are ignored by default when decoding contents, but this
behavior can be changed with the optional ``$ignoredNodeTypes`` argument of
behavior can be changed with the optional ``$decoderIgnoredNodeTypes`` argument of
the ``XmlEncoder`` class constructor.

Data with ``#comment`` keys are encoded to XML comments by default. This can be
changed with the optional ``$encoderIgnoredNodeTypes`` argument of the
``XmlEncoder`` class constructor.

.. versionadded:: 4.1
XML comments are ignored by default starting from Symfony 4.1.

Expand Down