Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[WIP] Reorganize the Serializer documentation#6428
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
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
File 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
2 changes: 1 addition & 1 deletioncomponents/index.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
8 changes: 5 additions & 3 deletionscomponents/map.rst.inc
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
105 changes: 105 additions & 0 deletionscomponents/serializer/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,105 @@ | ||
| .. 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 | ||
| ----------------- | ||
| You can see in the example above that we use two encoders: | ||
| * :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML | ||
| * :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON | ||
| The ``XmlEncoder`` | ||
| ~~~~~~~~~~~~~~~~~~ | ||
| This encoder transform arrays into XML and vice versa. | ||
| For example, we will guess that you have 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 `json_encode`_ and `json_decode`_ functions. | ||
| .. _json_encode: https://secure.php.net/manual/fr/function.json-encode.php | ||
| .. _json_decode: https://secure.php.net/manual/fr/function.json-decode.php | ||
| Custom encoders | ||
| --------------- | ||
| If you need to support another format than XML and JSON, you can create your own encoder. | ||
| We will guess that you want to serialize and deserialize Yaml. For that, we will use | ||
| :doc:`/components/yaml/index`:: | ||
| namespace App\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 'json' === $format; | ||
| } | ||
| public function decode($data, $format, array $context = array()) | ||
| { | ||
| return Yaml::parse($data); | ||
| } | ||
| public function supportsDecoding($format) | ||
| { | ||
| return 'json' === $format; | ||
| } | ||
| } | ||
| Then just pass it to your serializer:: | ||
| use Symfony\Component\Serializer\Serializer; | ||
| $serializer = new Serializer(array(), array(new App\Encoder\YamlEncoder())); | ||
| Now you'll be able to serialize and deserialize Yaml. |
9 changes: 9 additions & 0 deletionscomponents/serializer/index.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,9 @@ | ||
| Serializer | ||
| ========== | ||
| .. toctree:: | ||
| :maxdepth: 2 | ||
| introduction | ||
| encoders | ||
| normalizers |
99 changes: 14 additions & 85 deletionscomponents/serializer.rst → components/serializer/introduction.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
23 changes: 23 additions & 0 deletionscomponents/serializer/normalizers.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,23 @@ | ||
| .. index:: | ||
| single: Security, Normalizers | ||
| Normalizers | ||
| =========== | ||
| Normalizers turn **objects** into **arrays** and vice-versa. | ||
| They implement :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface` for normalizing (object to array) and :class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface` for denormalizing (array to object). | ||
| You can add new normalizers to a Serializer instance by using its first constructor argument:: | ||
| use Symfony\Component\Serializer\Serializer; | ||
| use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; | ||
| use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; | ||
| $normalizers = array(new ArrayDenormalizer(), new GetSetMethodNormalizer()); | ||
| $serializer = new Serializer($normalizers); | ||
| Built-in encoders | ||
| ----------------- | ||
| The Serializer Component provides several normalizers for most use cases: | ||
| * |
4 changes: 2 additions & 2 deletionscookbook/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
1 change: 1 addition & 0 deletionsredirection_map
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
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.