Serialization module: (de)serializing data to/from JSON & more

The Serialization module is included in Drupal 8 core.

It is built on top of the Symfony Serializer component.

The Serialization module provides a framework to add additional serialization formats simply by installing additional modules. Drupal 8 core's HAL module adds support for HAL+JSON, the CSV serialization contrib module adds CSV support.

Entity Pilot - content staging for Drupal 8

Installation

  • Download and extract the module
  • Add the php-encryption library using composer or Composer manager.
    • For composer - from your Drupal installation directory run composer require "defuse/php-encryption:~1.2.
    • For Composer manager, refer to its documentation

Getting started

Changing the way Serializer handles entities

Core supports serialization for content entities, their fields, and field items. It is easy to add support for other classes, and also to modify the way Serializer handles classes.

For altering how Serializer handles a field item, one example is EntityReferenceFieldItemNormalizer. For this, they've created a class file in a module's src/Normalizer directory, which...

How the Serializer works

The Serialization API depends on Symfony's Serializer component.

Serializer's separation of concerns

The serializer splits the work into two steps. First, it will normalize the object to an array. Then it will encode that array into the requested format.

Diagram showing object transformed to array by normalize method and array transformed to string by encode

This work is split across two interfaces, the NormalizerInterface and the EncoderInterface. When a Serializer is created, all of the Normalizer objects which might be necessary to handle the data are passed in, as well as all of the relevant Encoder objects.

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ComplexDataNormalizer(), new ListNormalizer(), new TypedDataNormalizer());

$serializer = new Serializer($normalizers, $encoders);

Serialization API

The Serialization API is new in Drupal 8. It is largely based on the Symfony Serializer component, so their documentation is also relevant.

For high-level information on what this module does, see the Serialization module documentation.

 Symfony documentation.

API features

Ordered by most to least frequently used APIs:

Subscribe with RSS Subscribe to RSS - serialization