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