Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

Symfony's Serializer component was added to Drupal 8. This makes it possible to output data in serialization formats like JSON and XML, which is important for REST services.

A Serializer is created with 1) an array of Normalizers, which take an object and convert it to an array/scalar, and 2) an array of Encoders, which take the normalized output and convert it to the desired format. Normalizers can specify both the object type and the format that they can handle. Encoders can specify the format. The correct Normalizer and Encoder are chosen for an object by iterating through the array and calling Normalizer::supportsNormalization($object, $format) or Encoder::supportsEncoding($format), respectively. The first match found is used, and the decision cached for other requests that have matching parameters.

One Serializer is added as a service to the DIC. This is used by page controllers, such as REST module's EntityResource::get(), to process entities or arrays to their serialized equivalent (e.g a JSON object).

Modules that define a serialization can add their Normalizers and Encoders to the main Serializer by registering them with the DIC. The priority attribute can be used to alter the order. Higher priority items are earlier in the array (so priority 5 would come before priority 2).

Here's for example serialization_test.services.yml:

services:
  serializer.normalizer.serialization_test:
    class: Drupal\serialization_test\SerializationTestNormalizer
    tags:
      - { name: normalizer }
  serializer.encoder.serialization_test:
    class: Drupal\serialization_test\SerializationTestEncoder
    tags:
      - { name: encoder, format: serialization_test}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done