Serializer API

Table of Contents
  1. Serializer API

    1. The SerializerGroup class

  2. Defining a new Serializer

1 - Serializer API

The serialization API is designed to be easily extensible by developers.
If you are writing your own serializer, you will typically subclass directly from either {@link org.apache.juneau.serializer.WriterSerializer} or {@link org.apache.juneau.serializer.OutputStreamSerializer}.

1.1 - The SerializerGroup class

The {@link org.apache.juneau.serializer.SerializerGroup} class represents a group of serializers registered with the media types they handle.

Features

The SerializerGroup class provides the following features:

Refer to {@link org.apache.juneau.serializer.SerializerGroup} for additional information.

2 - Defining a new Serializer

Defining a new serializer is quite simple if you subclass directly from {@link org.apache.juneau.serializer.WriterSerializer} or {@link org.apache.juneau.serializer.OutputStreamSerializer}.
In each case, you simply need to implement a single method and specify a {@link org.apache.juneau.annotation.Produces} annotation.

The following example shows a simple serializer that converts images to output streams using standard JRE classes.

@Produces("image/png,image/jpeg") public static class ImageSerializer extends OutputStreamSerializer { @Override public void serialize(Object o, OutputStream out, SerializerSession session) throws IOException, SerializeException { RenderedImage image = (RenderedImage)o; String mediaType = ctx.getMediaType(); ImageIO.write(image, mediaType.substring(mediaType.indexOf('/')+1), out); } }

Serializer that take advantage of the entire {@link org.apache.juneau.CoreObject} interface to be able to serialize arbitrary beans and POJOs is considerably more complex and outside the scope of this document.
If developing such a serializer, the best course of action would be to replicate what occurs in the {@link org.apache.juneau.json.JsonSerializer} class.