Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[Finishing][Serializer] Document the encoders#6976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
dbdbf68 [Serializer] Document the encoders
GuilhemN2c66c83 Merge branch 'ENCODERS' of https://github.com/Ener-Getick/symfony-doc…
weaverryan33e5b76 Moving files into the new structure
weaverryan73172c6 updating links
weaverryanf99663a removing cookbook entries
weaverryan10b2fa3 Minor language tweaks
weaverryanFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
9 changes: 4 additions & 5 deletionscomponents/serializer.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletionsserializer.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletionsserializer/custom_encoders.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| .. index:: | ||
| single: Serializer; Custom encoders | ||
| How to Create your Custom Encoder | ||
| ================================= | ||
| The :doc:`Serializer Component </components/serializer>` uses Normalizers | ||
| to transform any data to an array. Then, by leveraging *Encoders*, that data can | ||
| be convereted into any data-structure (e.g. JSON). | ||
| The Component provides several built-in encoders that are described | ||
| :doc:`in their own section </serializer/encoders>` but you may want | ||
| to use another structure that's not supported. | ||
| Creating a new encoder | ||
| ---------------------- | ||
| Imagine you want to serialize and deserialize Yaml. For that you'll have to | ||
| create your own encoder that uses the | ||
| :doc:`Yaml Component </components/yaml>`:: | ||
| namespace AppBundle\Encoder; | ||
| use Symfony\Component\Serializer\Encoder\DecoderInterface; | ||
| use Symfony\Component\Serializer\Encoder\EncoderInterface; | ||
| use Symfony\Component\Yaml\Yaml; | ||
| class YamlEncoder implements EncoderInterface, DecoderInterface | ||
| { | ||
| public function encode($data, $format, array $context = array()) | ||
| { | ||
| return Yaml::dump($data); | ||
| } | ||
| public function supportsEncoding($format) | ||
| { | ||
| return 'yaml' === $format; | ||
| } | ||
| public function decode($data, $format, array $context = array()) | ||
| { | ||
| return Yaml::parse($data); | ||
| } | ||
| public function supportsDecoding($format) | ||
| { | ||
| return 'yaml' === $format; | ||
| } | ||
| } | ||
| Registering it in your app | ||
| -------------------------- | ||
| If you use the Symfony Framework. then you probably want to register this encoder | ||
| as a service in your app. Then, you only need to tag it with ``serializer.encoder`` | ||
| to inject your custom encoder into the Serializer. | ||
| .. configuration-block:: | ||
| .. code-block:: yaml | ||
| # app/config/services.yml | ||
| services: | ||
| app.encoder.yaml: | ||
| class: AppBundle\Encoder\YamlEncoder | ||
| tags: | ||
| - { name: serializer.encoder } | ||
| .. code-block:: xml | ||
| <!-- app/config/services.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <service id="app.encoder.yaml" class="AppBundle\Encoder\YamlEncoder"> | ||
| <tag name="serializer.encoder" /> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // app/config/services.php | ||
| $container | ||
| ->register( | ||
| 'app.encoder.yaml', | ||
| 'AppBundle\Encoder\YamlEncoder' | ||
| ) | ||
| ->addTag('serializer.encoder') | ||
| ; | ||
| Now you'll be able to serialize and deserialize Yaml! | ||
| .. _tracker: https://github.com/symfony/symfony/issues |
63 changes: 63 additions & 0 deletionsserializer/encoders.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| .. index:: | ||
| single: Serializer, Encoders | ||
| Encoders | ||
| ======== | ||
| Encoders basically turn **arrays** into **formats** and vice versa. | ||
| 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). | ||
| You can add new encoders to a Serializer instance by using its second constructor argument:: | ||
| use Symfony\Component\Serializer\Serializer; | ||
| use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
| use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
| $encoders = array(new XmlEncoder(), new JsonEncoder()); | ||
| $serializer = new Serializer(array(), $encoders); | ||
| Built-in Encoders | ||
| ----------------- | ||
| Two encoders are used in the example above: | ||
| * :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML | ||
| * :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON | ||
| The ``XmlEncoder`` | ||
| ~~~~~~~~~~~~~~~~~~ | ||
| This encoder transforms arrays into XML and vice versa. | ||
| For example, take an object normalized as following:: | ||
| array('foo' => array(1, 2), 'bar' => true); | ||
| The ``XmlEncoder`` will encode this object like that:: | ||
| <?xml version="1.0"?> | ||
| <response> | ||
| <foo>1</foo> | ||
| <foo>2</foo> | ||
| <bar>1</bar> | ||
| </response> | ||
| Be aware that this encoder will consider keys beginning with ``@`` as attributes:: | ||
| $encoder = new XmlEncoder(); | ||
| $encoder->encode(array('foo' => array('@bar' => 'value'))); | ||
| // will return: | ||
| // <?xml version="1.0"?> | ||
| // <response> | ||
| // <foo bar="value" /> | ||
| // </response> | ||
| The ``JsonEncoder`` | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
| The ``JsonEncoder`` is much simpler and is based on the PHP | ||
| :phpfunction:`json_encode` and :phpfunction:`json_decode` functions. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.