Class SerializerRegistrar


  • public class SerializerRegistrar
    extends Object
    A collection of serializers. Once you register a serializer for a type T, list/set types of any depth with that type T as element type can be serialized without having to register anything else. You can override their serialization routine explicitly if you want though.

    Arrays are not supported out of the box. They'd require some boilerplate I don't want to write without a use case.

    Serializers for common types are registered implicitly.

    Instead of creating serializer for new types from scratch, you can instead map existing serializers to your new types. Eg if some value of type Foo has a string id, then you can do:

         
          registrar.registerMapped(Foo.class, String.class, Foo::fromId, Foo::getId);
         
     
    Author:
    Clément Fournier
    • Constructor Detail

      • SerializerRegistrar

        public SerializerRegistrar()
    • Method Detail

      • registerMapped

        public final <T,​U> void registerMapped​(Class<T> toRegister,
                                                     Class<U> existing,
                                                     Function<T,​U> toBase,
                                                     Function<U,​T> fromBase)
        Registers a new serializer for type [toRegister], which is based on an already registered serializer for [existing]. The new serializer is obtained using Serializer.map(Function, Function).
      • register

        @SafeVarargs
        public final <T> void register​(Serializer<T> serializer,
                                       org.apache.commons.lang3.reflect.Typed<T> firstType,
                                       org.apache.commons.lang3.reflect.Typed<T>... type)
        Registers a serializer suitable for one or more types. It can then be accessed on this registrar instance with getSerializer(Typed).
      • compositeSerializer

        public Serializer<TypedObject<?>> compositeSerializer()
        Returns a new serializer that uses all the serializers registered on this registrar instance to serialize any object. It throws IllegalStateExceptions if serializers are missing for a particular type.
      • getSerializer

        public final <T> Serializer<T> getSerializer​(Class<T> type)
        Gets a registered serializer for an a type. If the class is an array type and some component type has a registered serializer, a new array serializer will be derived and returned transparently.

        To get serializers for collection types, use rather getSerializer(Typed).

        Returns:
        A serializer, or null if none can be derived
      • getSerializer

        public final <T> Serializer<T> getSerializer​(org.apache.commons.lang3.reflect.Typed<T> typed)
        Typesafe version of getSerializer(Type).
        Parameters:
        typed - Type witness
      • getSerializer

        public final @Nullable Serializer<?> getSerializer​(Type genericType)
        Get a serializer for some generic type. If the type is generic and has at most one type argument, a best effort strategy will try to find a suitable serializer.
        Parameters:
        genericType - The type for which to get a serializer.
        Returns:
        A serializer, or null if none can be found