|
| 1 | +.. index:: |
| 2 | + single: Serializer, Encoders |
| 3 | + |
| 4 | +Encoders |
| 5 | +======== |
| 6 | + |
| 7 | +Encoders basically turn **arrays** into **formats** and vice versa. |
| 8 | +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). |
| 9 | + |
| 10 | +You can add new encoders to a Serializer instance by using its second constructor argument:: |
| 11 | + |
| 12 | + use Symfony\Component\Serializer\Serializer; |
| 13 | + use Symfony\Component\Serializer\Encoder\XmlEncoder; |
| 14 | + use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 15 | + |
| 16 | + $encoders = array(new XmlEncoder(), new JsonEncoder()); |
| 17 | + $serializer = new Serializer(array(), $encoders); |
| 18 | + |
| 19 | +Built-in encoders |
| 20 | +----------------- |
| 21 | + |
| 22 | +You can see in the example above that we use two encoders: |
| 23 | + |
| 24 | +* :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML |
| 25 | +* :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON |
| 26 | + |
| 27 | +The ``XmlEncoder`` |
| 28 | +~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +This encoder transform arrays into XML and vice versa. |
| 31 | + |
| 32 | +For example, we will guess that you have an object normalized as following:: |
| 33 | + |
| 34 | + array('foo' => array(1, 2), 'bar' => true); |
| 35 | + |
| 36 | +The ``XmlEncoder`` will encode this object like that:: |
| 37 | + |
| 38 | + <?xml version="1.0"?> |
| 39 | + <response> |
| 40 | + <foo>1</foo> |
| 41 | + <foo>2</foo> |
| 42 | + <bar>1</bar> |
| 43 | + </response> |
| 44 | + |
| 45 | +Be aware that this encoder will consider keys beginning with ``@`` as attributes:: |
| 46 | + |
| 47 | + $encoder = new XmlEncoder(); |
| 48 | + $encoder->encode(array('foo' => array('@bar' => 'value'))); |
| 49 | + // will return: |
| 50 | + // <?xml version="1.0"?> |
| 51 | + // <response> |
| 52 | + // <foo bar="value" /> |
| 53 | + // </response> |
| 54 | + |
| 55 | +The ``JsonEncoder`` |
| 56 | +~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +The ``JsonEncoder`` is much simpler and is based on the PHP `json_encode`_ and `json_decode`_ functions. |
| 59 | + |
| 60 | +.. _json_encode: https://secure.php.net/manual/fr/function.json-encode.php |
| 61 | +.. _json_decode: https://secure.php.net/manual/fr/function.json-decode.php |
| 62 | + |
| 63 | +Custom encoders |
| 64 | +--------------- |
| 65 | + |
| 66 | +If you need to support another format than XML and JSON, you can create your own encoder. |
| 67 | +We will guess that you want to serialize and deserialize Yaml. For that, we will use |
| 68 | +:doc:`/components/yaml/index`:: |
| 69 | + |
| 70 | + namespace App\Encoder; |
| 71 | + |
| 72 | + use Symfony\Component\Serializer\Encoder\DecoderInterface; |
| 73 | + use Symfony\Component\Serializer\Encoder\EncoderInterface; |
| 74 | + use Symfony\Component\Yaml\Yaml; |
| 75 | + |
| 76 | + class YamlEncoder implements EncoderInterface, DecoderInterface |
| 77 | + { |
| 78 | + public function encode($data, $format, array $context = array()) |
| 79 | + { |
| 80 | + return Yaml::dump($data); |
| 81 | + } |
| 82 | + |
| 83 | + public function supportsEncoding($format) |
| 84 | + { |
| 85 | + return 'json' === $format; |
| 86 | + } |
| 87 | + |
| 88 | + public function decode($data, $format, array $context = array()) |
| 89 | + { |
| 90 | + return Yaml::parse($data); |
| 91 | + } |
| 92 | + |
| 93 | + public function supportsDecoding($format) |
| 94 | + { |
| 95 | + return 'json' === $format; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +Then just pass it to your serializer:: |
| 100 | + |
| 101 | + use Symfony\Component\Serializer\Serializer; |
| 102 | + |
| 103 | + $serializer = new Serializer(array(), array(new App\Encoder\YamlEncoder())); |
| 104 | + |
| 105 | +Now you'll be able to serialize and deserialize Yaml. |
0 commit comments