diff --git a/core/serialization.md b/core/serialization.md index 030e031116c..e4e21650b62 100644 --- a/core/serialization.md +++ b/core/serialization.md @@ -8,12 +8,12 @@ The main serialization process has two stages: ![Serializer workflow](images/SerializerWorkflow.png) -> As you can see in the picture above, an array is used as a man in the middle. This way, Encoders will only deal with turning specific formats into arrays and vice versa. The same way, Normalizers will deal with turning specific objects into arrays and vice versa. +> As you can see in the picture above, an array is used as a man-in-the-middle. This way, Encoders will only deal with turning specific formats into arrays and vice versa. The same way, Normalizers will deal with turning specific objects into arrays and vice versa. -- [The Symfony documentation](https://symfony.com/doc/current/components/serializer.html) Unlike Symfony itself, API Platform leverages custom normalizers, its router and the [data provider](data-providers.md) system to do an advanced transformation. Metadata are added to the generated document including links, type information, pagination data or available filters. -The API Platform Serializer is extendable, you can register custom normalizers and encoders to support other formats. You can also decorate existing normalizers to customize their behaviors. +The API Platform Serializer is extendable. You can register custom normalizers and encoders in order to support other formats. You can also decorate existing normalizers to customize their behaviors. ## Available Serializers @@ -30,21 +30,19 @@ JSON-LD, or JavaScript Object Notation for Linked Data, is a method of encoding ## The Serialization Context, Groups and Relations -API Platform allows to specify the `$context` parameter used by the Symfony Serializer. This context has a handy -`groups` key allowing to choose which attributes of the resource are exposed during the normalization (read) and denormalization -(write) process. +API Platform allows you to specify the `$context` variable used by the Symfony Serializer. This variable is an associative array that has a handy `groups` key allowing you to choose which attributes of the resource are exposed during the normalization (read) and denormalization (write) processes. It relies on the [serialization (and deserialization) groups](https://symfony.com/doc/current/components/serializer.html#attributes-groups) feature of the Symfony Serializer component. -In addition to groups, you can use any option supported by the Symfony Serializer such as [`enable_max_depth`](https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth) +In addition to groups, you can use any option supported by the Symfony Serializer. For example, you can use [`enable_max_depth`](https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth) to limit the serialization depth. ### Configuration -Just like other Symfony and API Platform components, the Serializer can be configured using annotations, XML and YAML. -As annotations are easy to understand and allow to group code and configuration, we will use them in the following examples. +Just like other Symfony and API Platform components, the Serializer component can be configured using annotations, XML or YAML. +Since annotations are easy to understand, we will use them in the following examples. -However, if you don't use the official distribution of API Platform, don't forget to enable annotation support in the serializer +Note: if you aren't using the official distribution of API Platform, you will need to enable annotation support in the serializer configuration: ```yaml @@ -58,7 +56,10 @@ all set! ## Using Serialization Groups -It is really simple to specify what groups to use in the API system: +It is simple to specify what groups to use in the API system: + +1. Add the `normalizationContext` and `denormalizationContext` annotation properties to the `@ApiResource` annotation, and specify which groups to use. Here you see that we add `read` and `write`, respectively. You can use any group names you wish. +2. Apply the `@Groups` annotation to properties in the object. ```php