T - the base type of the objects this factory can create. T can be of type Converter,
Serializer or Deserializer.public interface Factory<T>
DefaultConverters. 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 of TypeUtil class that provides operations to
work with generic types. However this class might change in the future, in order to provide a better API.Converter,
ChainedFactory,
Serializer,
Deserializer| Modifier and Type | Method and Description |
|---|---|
T |
create(Type type,
Genson genson)
Implementations of this method must try to create an instance of type T based on the
parameter "type".
|
T create(Type type, Genson genson)
type - used to build an instance of T.Copyright © 2019. All rights reserved.