@@ -606,7 +606,26 @@ There are several types of normalizers available:
606
606
Encoders
607
607
--------
608
608
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:
610
629
611
630
:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `
612
631
This class encodes and decodes data in JSON _.
@@ -624,6 +643,56 @@ The Serializer component supports many formats out of the box:
624
643
All these encoders are enabled by default when using the Symfony Standard Edition
625
644
with the serializer enabled.
626
645
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
+
627
696
.. _component-serializer-handling-circular-references :
628
697
629
698
Handling Circular References
0 commit comments