Last updated December 30, 2013. Created by linclark on January 27, 2013.
Edited by yched. 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')
      ->addTag('encoder');

    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, http://drupal.org/node/1897612

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