Class SerializerSet

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

public final 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.swaps(TemporalCalendarSwap.IsoLocalDateTime.class)) .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"); // Serialize a bean to JSON text AddressBook addressBook = new AddressBook(); // Bean to serialize. String json = serializer.serialize(addressBook);

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

  • Method Details

    • create

      public static SerializerSet.Builder create(BeanStore beanStore)
      Static creator.
      Parameters:
      beanStore - The bean store to use for creating beans.
      Returns:
      A new builder for this object.
    • create

      public static SerializerSet.Builder create()
      Static creator.
      Returns:
      A new builder for this object.
    • copy

      Creates a copy of this serializer group.
      Returns:
      A new copy of this serializer group.
    • getSerializerMatch

      public SerializerMatch getSerializerMatch(String acceptHeader)
      Searches the group for a serializer that can handle the specified Accept value.

      The accept value complies with the syntax described in RFC2616, Section 14.1, as described below:

      Accept = "Accept" ":" #( media-range [ accept-params ] ) media-range = ( "*\/*" | ( type "/" "*" ) | ( type "/" subtype ) ) *( ";" parameter ) accept-params = ";" "q" "=" qvalue *( accept-extension ) accept-extension = ";" token [ "=" ( token | quoted-string ) ]

      The returned object includes both the serializer and media type that matched.

      Parameters:
      acceptHeader - The HTTP Accept header string.
      Returns:
      The serializer and media type that matched the accept header, or null if no match was made.
    • getSerializerMatch

      Same as getSerializerMatch(String) but matches using a MediaType instance.
      Parameters:
      mediaType - The HTTP media type.
      Returns:
      The serializer and media type that matched the media type, or null if no match was made.
    • getSerializer

      public Serializer getSerializer(String acceptHeader)
      Same as getSerializerMatch(String) but returns just the matched serializer.
      Parameters:
      acceptHeader - The HTTP Accept header string.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getSerializer

      public Serializer getSerializer(MediaType mediaType)
      Same as getSerializerMatch(MediaType) but returns just the matched serializer.
      Parameters:
      mediaType - The HTTP media type.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getWriterSerializer

      Same as getSerializer(String), but casts it to a WriterSerializer.
      Parameters:
      acceptHeader - The HTTP Accept header string.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getWriterSerializer

      Same as getSerializer(MediaType), but casts it to a WriterSerializer.
      Parameters:
      mediaType - The HTTP media type.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getStreamSerializer

      Same as getSerializer(String), but casts it to an OutputStreamSerializer.
      Parameters:
      acceptHeader - The HTTP Accept header string.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getStreamSerializer

      Parameters:
      mediaType - The HTTP media type.
      Returns:
      The serializer that matched the accept header, or null if no match was made.
    • getSupportedMediaTypes

      Returns the media types that all serializers in this group can handle.

      Entries are ordered in the same order as the serializers in the group.

      Returns:
      An unmodifiable list of media types.
    • getSerializers

      Returns a copy of the serializers in this group.
      Returns:
      An unmodifiable list of serializers in this group.
    • isEmpty

      public boolean isEmpty()
      Returns true if this group contains no serializers.
      Returns:
      true if this group contains no serializers.