Interface Factory<T>
-
- Type Parameters:
T- the base type of the objects this factory can create. T can be of type Converter, Serializer or Deserializer.
- All Known Implementing Classes:
AbstractBeanDescriptorProvider.ContextualFactoryDecorator,BasicConvertersFactory,BeanViewConverter.BeanViewConverterFactory,ChainedFactory,CircularClassReferenceConverterFactory,ClassConverter.Factory,ClassMetadataConverter.ClassMetadataConverterFactory,ComparableConverter.Factory,DefaultConverters.ArrayConverterFactory,DefaultConverters.CalendarConverterFactory,DefaultConverters.CollectionConverterFactory,DefaultConverters.EnumConverterFactory,DefaultConverters.MapConverterFactory,DefaultConverters.OptionalConverterFactory,DefaultConverters.PrimitiveConverterFactory,DefaultConverters.SingleValueAsListFactory,DefaultConverters.UntypedConverterFactory,EnumConverter.Factory,MapConverter.Factory,MissingClassConverter.Factory,NullConverterFactory,RuntimeTypeConverter.RuntimeTypeConverterFactory,SerializationSupportConverter.Factory,ThrowableConverter.Factory
public interface Factory<T>Factory interface must be implemented by classes who want to act as factories and create instances of Converter/Serializer/Deserializer. Implementations will be used as Converter, Serializer and Deserializer factories. So the type T will be something like Converter<Integer> but the type argument of method create will correspond to Integer or a subclass of Integer. As an example you can have a look at factories fromDefaultConverters. Here is an example with a custom converter and factory for enums.public static class EnumConverter<T extends Enum<T>> implements Converter<T> { private final Class<T> eClass; public EnumConverter(Class<T> eClass) { this.eClass = eClass; } @Override public void serialize(T obj, ObjectWriter writer, Context ctx) { writer.writeUnsafeValue(obj.name()); } @Override public T deserialize(ObjectReader reader, Context ctx) { return Enum.valueOf(eClass, reader.valueAsString()); } } public final static class EnumConverterFactory implements Factory<Converter<? extends Enum<?>>> { public final static EnumConverterFactory instance = new EnumConverterFactory(); private EnumConverterFactory() { } @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Converter<Enum<?>> create(Type type, Genson genson) { Class<?> rawClass = TypeUtil.getRawClass(type); return rawClass.isEnum() || Enum.class.isAssignableFrom(rawClass) ? new EnumConverter( rawClass) : null; } };Note the use ofTypeUtilclass that provides operations to work with generic types. However this class might change in the future, in order to provide a better API.- Author:
- Eugen Cepoi
- See Also:
Converter,ChainedFactory,Serializer,Deserializer
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Tcreate(Type type, Genson genson)Implementations of this method must try to create an instance of type T based on the parameter "type".
-
-
-
Method Detail
-
create
T create(Type type, Genson genson)
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.- 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).
-
-