Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1878 | jmachado | 1 | package com.owlike.genson; |
2 | |||
3 | import java.lang.reflect.Type; |
||
4 | |||
5 | /** |
||
6 | * Factory interface must be implemented by classes who want to act as factories and create |
||
7 | * instances of Converter/Serializer/Deserializer. Implementations will be used as Converter, |
||
8 | * Serializer and Deserializer factories. So the type T will be something like |
||
9 | * Converter<Integer> but the type argument of method create will correspond to Integer <u>or |
||
10 | * a subclass of Integer</u>. |
||
11 | * <p/> |
||
12 | * As an example you can have a look at factories from {@link com.owlike.genson.convert.DefaultConverters |
||
13 | * DefaultConverters}. Here is an example with a custom converter and factory for enums. |
||
14 | * <p/> |
||
15 | * <pre> |
||
16 | * public static class EnumConverter<T extends Enum<T>> implements Converter<T> { |
||
17 | * private final Class<T> eClass; |
||
18 | * |
||
19 | * public EnumConverter(Class<T> eClass) { |
||
20 | * this.eClass = eClass; |
||
21 | * } |
||
22 | * |
||
23 | * @Override |
||
24 | * public void serialize(T obj, ObjectWriter writer, Context ctx) { |
||
25 | * writer.writeUnsafeValue(obj.name()); |
||
26 | * } |
||
27 | * |
||
28 | * @Override |
||
29 | * public T deserialize(ObjectReader reader, Context ctx) { |
||
30 | * return Enum.valueOf(eClass, reader.valueAsString()); |
||
31 | * } |
||
32 | * } |
||
33 | * |
||
34 | * public final static class EnumConverterFactory implements Factory<Converter<? extends Enum<?>>> { |
||
35 | * public final static EnumConverterFactory instance = new EnumConverterFactory(); |
||
36 | * |
||
37 | * private EnumConverterFactory() { |
||
38 | * } |
||
39 | * |
||
40 | * @SuppressWarnings({ "rawtypes", "unchecked" }) |
||
41 | * @Override |
||
42 | * public Converter<Enum<?>> create(Type type, Genson genson) { |
||
43 | * Class<?> rawClass = TypeUtil.getRawClass(type); |
||
44 | * return rawClass.isEnum() || Enum.class.isAssignableFrom(rawClass) ? new EnumConverter( |
||
45 | * rawClass) : null; |
||
46 | * } |
||
47 | * }; |
||
48 | * </pre> |
||
49 | * <p/> |
||
50 | * Note the use of {@link com.owlike.genson.reflect.TypeUtil TypeUtil} class that provides operations to |
||
51 | * work with generic types. However this class might change in the future, in order to provide a better API. |
||
52 | * |
||
53 | * @param <T> the base type of the objects this factory can create. T can be of type Converter, |
||
54 | * Serializer or Deserializer. |
||
55 | * @author eugen |
||
56 | * @see com.owlike.genson.Converter |
||
57 | * @see com.owlike.genson.convert.ChainedFactory ChainedFactory |
||
58 | * @see com.owlike.genson.Serializer |
||
59 | * @see com.owlike.genson.Deserializer |
||
60 | */ |
||
61 | public interface Factory<T> { |
||
62 | /** |
||
63 | * Implementations of this method must try to create an instance of type T based on the |
||
64 | * parameter "type". If this factory can not create an object of type T for parameter type then |
||
65 | * it must return null. |
||
66 | * |
||
67 | * @param type used to build an instance of T. |
||
68 | * @return null if it doesn't support this type or an instance of T (or a subclass). |
||
69 | */ |
||
70 | public T create(1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Genson genson); |
||
71 | } |