Skip to content

[Serializer] feature: add the context key and add a link to the encoders in serializer to avoid duplication. #9296

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

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
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
71 changes: 70 additions & 1 deletion components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,26 @@ There are several types of normalizers available:
Encoders
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that we show all the information about Normalizers in this article ... I think the change should be the opposite: delete the serializer/encoders.rst article entirely and move its contents to this Encoders section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do

--------

The Serializer component supports many formats out of the box:
Encoders basically turn **arrays** into **formats** and vice versa.
They implement
:class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for
encoding (array to format) and
:class:`Symfony\\Component\\Serializer\\Encoder\\DecoderInterface` for
decoding (format to array).

You can add new encoders to a Serializer instance by using its second constructor argument::

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

$encoders = array(new XmlEncoder(), new JsonEncoder());
$serializer = new Serializer(array(), $encoders);

Built-in Encoders
~~~~~~~~~~~~~~~~~

The Serializer component provides built-in encoders:

:class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`
This class encodes and decodes data in JSON_.
Expand All @@ -623,6 +642,56 @@ The Serializer component supports many formats out of the box:
All these encoders are enabled by default when using the Symfony Standard Edition
with the serializer enabled.

The ``JsonEncoder``
~~~~~~~~~~~~~~~~~~~

The ``JsonEncoder`` encodes to and decodes from JSON strings, based on the PHP
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.

The ``CsvEncoder``
~~~~~~~~~~~~~~~~~~~

The ``CsvEncoder`` encodes to and decodes from CSV.

You can pass the context key ``as_collection`` in order to have the results always as a collection.

The ``XmlEncoder``
~~~~~~~~~~~~~~~~~~

This encoder transforms arrays into XML and vice versa.

For example, take an object normalized as following::

array('foo' => array(1, 2), 'bar' => true);

The ``XmlEncoder`` will encode this object like that::

<?xml version="1.0"?>
<response>
<foo>1</foo>
<foo>2</foo>
<bar>1</bar>
</response>

Be aware that this encoder will consider keys beginning with ``@`` as attributes::

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

You can pass the context key ``as_collection`` in order to have the results always as a collection.

The ``YamlEncoder``
~~~~~~~~~~~~~~~~~~~

This encoder requires the :doc:`Yaml Component </components/yaml>` and
transforms from and to Yaml.


.. _component-serializer-handling-circular-references:

Handling Circular References
Expand Down
1 change: 0 additions & 1 deletion serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ take a look at how this bundle works.
.. toctree::
:maxdepth: 1

serializer/encoders
serializer/custom_encoders

.. _`APCu`: https://github.com/krakjoe/apcu
Expand Down
2 changes: 1 addition & 1 deletion serializer/custom_encoders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ to transform any data to an array. Then, by leveraging *Encoders*, that data can
be converted into any data-structure (e.g. JSON).

The Component provides several built-in encoders that are described
:doc:`in their own section </serializer/encoders>` but you may want
:doc:`in the serializer component </components/serializer>` but you may want
to use another structure that's not supported.

Creating a new encoder
Expand Down
71 changes: 0 additions & 71 deletions serializer/encoders.rst

This file was deleted.