Last updated December 30, 2013. Created on January 27, 2013.
Edited by yched, linclark. Log in to edit this page.

Core supports JSON and XML, as well as HAL-JSON. It is easy to add support for other formats.

Adding basic support

As explained in How the Serializer works, the serialization process has two parts, normalization and encoding.

If the array structure created by core's default Normalizers is acceptable for your format, then you can simply add an Encoder. For example, this is how support for YAML is added to core.

  1. Create an encoder
    Your Encoder should implement EncoderInterface.
  2. Register the encoder with the container
    Any service added to the container which is tagged with 'encoder' will be added to the encoder array which is passed into the Serializer. This code is placed in your module's Bundle class in the build() method.
    $container->register('serializer.encoder.yaml', 'Drupal\Core\Serialization\YamlEncoder')

    Additionally, if you want this format to be configurable via REST module, you should add a format attribute to the encoder tag. The array should be keyed by the format short name and the value should be the human readable name, which will be used in the REST UI.

    $container->register('serializer.encoder.yaml', 'Drupal\Core\Serialization\YamlEncoder')
      ->addTag('encoder', array('format' => array('yaml' => 'YAML')));
  3. If the MIME type of your format isn't registered, register it
    The default MIME types are defined in Request::initializeFormats(). If the MIME type of your format is not listed there, you need to add it to the Request object using Request::setFormat().

    This can be done with an event subscriber which is listening for onRequestEvent.

    // Insert example once YAML patch is finalized,

Looking for support? Visit the forums, or join #drupal-support in IRC.