Skip to content

Commit ada8440

Browse files
committed
minor #9296 [Serializer] feature: add the context key and add a link to the encoders in serializer to avoid duplication. (Simperfit)
This PR was merged into the master branch. Discussion ---------- [Serializer] feature: add the context key and add a link to the encoders in serializer to avoid duplication. Fixes #9384 and #9383. Commits ------- e6d3f77 feature: add the context key documentation and remove encoders documentation
2 parents befb90e + e6d3f77 commit ada8440

File tree

4 files changed

+71
-74
lines changed

4 files changed

+71
-74
lines changed

components/serializer.rst

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,26 @@ There are several types of normalizers available:
606606
Encoders
607607
--------
608608

609-
The Serializer component supports many formats out of the box:
609+
Encoders basically turn **arrays** into **formats** and vice versa.
610+
They implement
611+
:class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for
612+
encoding (array to format) and
613+
:class:`Symfony\\Component\\Serializer\\Encoder\\DecoderInterface` for
614+
decoding (format to array).
615+
616+
You can add new encoders to a Serializer instance by using its second constructor argument::
617+
618+
use Symfony\Component\Serializer\Serializer;
619+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
620+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
621+
622+
$encoders = array(new XmlEncoder(), new JsonEncoder());
623+
$serializer = new Serializer(array(), $encoders);
624+
625+
Built-in Encoders
626+
~~~~~~~~~~~~~~~~~
627+
628+
The Serializer component provides built-in encoders:
610629

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

646+
The ``JsonEncoder``
647+
~~~~~~~~~~~~~~~~~~~
648+
649+
The ``JsonEncoder`` encodes to and decodes from JSON strings, based on the PHP
650+
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.
651+
652+
The ``CsvEncoder``
653+
~~~~~~~~~~~~~~~~~~~
654+
655+
The ``CsvEncoder`` encodes to and decodes from CSV.
656+
657+
You can pass the context key ``as_collection`` in order to have the results always as a collection.
658+
659+
The ``XmlEncoder``
660+
~~~~~~~~~~~~~~~~~~
661+
662+
This encoder transforms arrays into XML and vice versa.
663+
664+
For example, take an object normalized as following::
665+
666+
array('foo' => array(1, 2), 'bar' => true);
667+
668+
The ``XmlEncoder`` will encode this object like that::
669+
670+
<?xml version="1.0"?>
671+
<response>
672+
<foo>1</foo>
673+
<foo>2</foo>
674+
<bar>1</bar>
675+
</response>
676+
677+
Be aware that this encoder will consider keys beginning with ``@`` as attributes::
678+
679+
$encoder = new XmlEncoder();
680+
$encoder->encode(array('foo' => array('@bar' => 'value')));
681+
// will return:
682+
// <?xml version="1.0"?>
683+
// <response>
684+
// <foo bar="value" />
685+
// </response>
686+
687+
You can pass the context key ``as_collection`` in order to have the results always as a collection.
688+
689+
The ``YamlEncoder``
690+
~~~~~~~~~~~~~~~~~~~
691+
692+
This encoder requires the :doc:`Yaml Component </components/yaml>` and
693+
transforms from and to Yaml.
694+
695+
627696
.. _component-serializer-handling-circular-references:
628697

629698
Handling Circular References

serializer.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ take a look at how this bundle works.
203203
.. toctree::
204204
:maxdepth: 1
205205

206-
serializer/encoders
207206
serializer/custom_encoders
208207

209208
.. _`APCu`: https://github.com/krakjoe/apcu

serializer/custom_encoders.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ to transform any data to an array. Then, by leveraging *Encoders*, that data can
99
be converted into any data-structure (e.g. JSON).
1010

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

1515
Creating a new encoder

serializer/encoders.rst

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)