|
| 1 | +.. index:: |
| 2 | + single: Serializer; Custom encoders |
| 3 | + |
| 4 | +How to Create your Custom Encoder |
| 5 | +================================= |
| 6 | + |
| 7 | +The :doc:`Serializer Component </components/serializer/index>` uses Normalizers |
| 8 | +to transform any data to an array that can be then converted in whatever |
| 9 | +data-structured language you want thanks to Encoders. |
| 10 | + |
| 11 | +The Component provides several built-in encoders that are described |
| 12 | +:doc:`in their own section </components/serializer/encoders>` but you may want |
| 13 | +to use another language not supported. |
| 14 | + |
| 15 | +Creating a new encoder |
| 16 | +---------------------- |
| 17 | + |
| 18 | +Imagine you want to serialize and deserialize Yaml. For that you'll have to |
| 19 | +create your own encoder that may use the |
| 20 | +:doc:`Yaml Component </components/yaml/index>`:: |
| 21 | + |
| 22 | + namespace AppBundle\Encoder; |
| 23 | + |
| 24 | + use Symfony\Component\Serializer\Encoder\DecoderInterface; |
| 25 | + use Symfony\Component\Serializer\Encoder\EncoderInterface; |
| 26 | + use Symfony\Component\Yaml\Yaml; |
| 27 | + |
| 28 | + class YamlEncoder implements EncoderInterface, DecoderInterface |
| 29 | + { |
| 30 | + public function encode($data, $format, array $context = array()) |
| 31 | + { |
| 32 | + return Yaml::dump($data); |
| 33 | + } |
| 34 | + |
| 35 | + public function supportsEncoding($format) |
| 36 | + { |
| 37 | + return 'yaml' === $format; |
| 38 | + } |
| 39 | + |
| 40 | + public function decode($data, $format, array $context = array()) |
| 41 | + { |
| 42 | + return Yaml::parse($data); |
| 43 | + } |
| 44 | + |
| 45 | + public function supportsDecoding($format) |
| 46 | + { |
| 47 | + return 'yaml' === $format; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | +Registering it in your app |
| 52 | +-------------------------- |
| 53 | + |
| 54 | +If you use the Symfony Framework then you probably want to register this encoder |
| 55 | +as a service in your app. Then you only need to tag it as `serializer.encoder` and it will be |
| 56 | +injected in the Serializer. |
| 57 | + |
| 58 | +.. configuration-block:: |
| 59 | + |
| 60 | + .. code-block:: yaml |
| 61 | +
|
| 62 | + # app/config/services.yml |
| 63 | + services: |
| 64 | + app.encoder.yaml: |
| 65 | + class: AppBundle\Encoder\YamlEncoder |
| 66 | + tags: |
| 67 | + - { name: serializer.encoder } |
| 68 | +
|
| 69 | + .. code-block:: xml |
| 70 | +
|
| 71 | + <!-- app/config/services.xml --> |
| 72 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 73 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 74 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 75 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 76 | +
|
| 77 | + <services> |
| 78 | + <service id="app.encoder.yaml" class="AppBundle\Encoder\YamlEncoder"> |
| 79 | + <tag name="serializer.encoder" /> |
| 80 | + </service> |
| 81 | + </services> |
| 82 | + </container> |
| 83 | +
|
| 84 | + .. code-block:: php |
| 85 | +
|
| 86 | + // app/config/services.php |
| 87 | + $container |
| 88 | + ->register( |
| 89 | + 'app.encoder.yaml', |
| 90 | + 'AppBundle\Encoder\YamlEncoder' |
| 91 | + ) |
| 92 | + ->addTag('serializer.encoder') |
| 93 | + ; |
| 94 | +
|
| 95 | +Now you'll be able to serialize and deserialize Yaml! |
| 96 | + |
| 97 | +.. _tracker: https://github.com/symfony/symfony/issues |
0 commit comments