Skip to content

Commit e6d3f77

Browse files
SimperfitAmrouche Hamza
authored and
Amrouche Hamza
committed
feature: add the context key documentation and remove encoders documentation
1 parent 5bb7c53 commit e6d3f77

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
@@ -605,7 +605,26 @@ There are several types of normalizers available:
605605
Encoders
606606
--------
607607

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

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

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

628697
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)