Class SerializerSet

java.lang.Object
org.apache.juneau.marshall.serializer.SerializerSet

public class SerializerSet extends Object
Represents a group of Serializers that can be looked up by media type.
Description
Provides the following features:
  • Finds serializers based on HTTP Accept header values.
  • Sets common properties on all serializers in a single method call.
  • Clones existing groups and all serializers within the group in a single method call.
Match ordering
Serializers are matched against Accept strings in the order they exist in this group.

Adding new entries will cause the entries to be prepended to the group. This allows for previous serializers to be overridden through subsequent calls.

For example, calling g.append(S1.class,S2.class).append(S3.class,S4.class) will result in the order S3, S4, S1, S2.

Example:

// Construct a new serializer group SerializerSet serializers = SerializerSet.create(); .add(JsonSerializer.class, UrlEncodingSerializer.class) // Add some serializers to it .forEach(x -> x.calendarFormat(ISO_LOCAL_DATE_TIME)) .forEachWS(x -> x.ws()) .build(); // Find the appropriate serializer by Accept type WriterSerializer serializer = serializers .getWriterSerializer("text/foo, text/json;q=0.8, text/*;q:0.6, *\/*;q=0.0") .orElseThrow(); // Throws if no serializer matched the Accept header. // Serialize a bean to JSON text AddressBook addressBook = new AddressBook(); // Bean to serialize. String json = serializer.write(addressBook);

Notes:
  • This class is thread safe and reusable.
See Also: