Class BasicConvertersFactory

  • All Implemented Interfaces:
    Factory<Converter<?>>

    public class BasicConvertersFactory
    extends Object
    implements Factory<Converter<?>>
    This is the base factory that will create converters based on the default ones and on custom Serializer, Deserializer and Converter. But it also uses factories (default and custom) and BeanDescriptorProvider that is responsible of creating bean converters.

    When you ask for a Converter it will

      1. Lookup in the registered Serializers for one that is parameterized with the current type, if found we finished (it takes the first one, so the order matters).
      2. Else we will try the factories by searching the ones that can create and instance of Serializer<CurrentType> (again the order is very important). We continue while they return null.
      3. If no factory could create an instance we will use BeanDescriptorProvider.
    • We apply all the same logic a second time for Deserializer.
    • If they are both an instance of Converter then we return one of them
    • Otherwise we will wrap both into a Converter.

    Note that the create method from the registered factories will only be called if the type with which they are parameterized is assignable from the current type. For example, if we look for a serializer of Integer then Factory<Converter<Integer>> and Factory<Serializer<Object>> match both, the first registered will be used.

    Author:
    Eugen Cepoi
    • Method Detail

      • create

        public Converter<?> create​(Type type,
                                   Genson genson)
        Description copied from interface: Factory
        Implementations of this method must try to create an instance of type T based on the parameter "type". If this factory can not create an object of type T for parameter type then it must return null.
        Specified by:
        create in interface Factory<Converter<?>>
        Parameters:
        type - used to build an instance of T.
        Returns:
        null if it doesn't support this type or an instance of T (or a subclass).
      • provide

        protected <T> T provide​(Class<T> forClass,
                                Type withParameterType,
                                Map<Type,​? extends T> fromTypeMap,
                                Genson genson)