Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1878 | jmachado | 1 | package com.owlike.genson.convert; |
2 | |||
3 | import java.lang.reflect.Type; |
||
4 | import java.util.List; |
||
5 | |||
6 | import com.owlike.genson.BeanView; |
||
7 | import com.owlike.genson.Context; |
||
8 | import com.owlike.genson.Converter; |
||
9 | import com.owlike.genson.Genson; |
||
10 | import com.owlike.genson.Wrapper; |
||
11 | import com.owlike.genson.annotation.HandleBeanView; |
||
12 | import com.owlike.genson.reflect.BeanDescriptor; |
||
13 | import com.owlike.genson.reflect.BeanViewDescriptorProvider; |
||
14 | import com.owlike.genson.reflect.TypeUtil; |
||
15 | import com.owlike.genson.stream.ObjectReader; |
||
16 | import com.owlike.genson.stream.ObjectWriter; |
||
17 | |||
18 | /** |
||
19 | * Converter responsible of applying the BeanView mechanism. |
||
20 | * |
||
21 | * @param <T> type of objects this BeanViewConverter can handle. |
||
22 | * @author eugen |
||
23 | * @see com.owlike.genson.reflect.BeanViewDescriptorProvider BeanViewDescriptorProvider |
||
24 | * @see com.owlike.genson.BeanView BeanView |
||
25 | */ |
||
26 | public class BeanViewConverter<T> extends Wrapper<Converter<T>> implements Converter<T> { |
||
27 | |||
28 | public static class BeanViewConverterFactory extends ChainedFactory { |
||
29 | private final BeanViewDescriptorProvider provider; |
||
30 | |||
31 | public BeanViewConverterFactory(BeanViewDescriptorProvider provider) { |
||
32 | this.provider = provider; |
||
33 | } |
||
34 | |||
35 | @1.5.0/docs/api/java/lang/SuppressWarnings.html">SuppressWarnings({"rawtypes", "unchecked"}) |
||
36 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
37 | protected Converter<?> create(1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Genson genson, Converter<?> nextConverter) { |
||
38 | if (!Wrapper.toAnnotatedElement(nextConverter).isAnnotationPresent( |
||
39 | HandleBeanView.class)) |
||
40 | // TODO as we link an instance to a type, we may optimize things, but for the moment it is okay |
||
41 | // lets see if this feature is used |
||
42 | return new BeanViewConverter(type, provider, nextConverter); |
||
43 | return nextConverter; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | private final BeanViewDescriptorProvider provider; |
||
48 | private final 1.5.0/docs/api/java/lang/reflect/Type.html">Type type; |
||
49 | |||
50 | public BeanViewConverter(1.5.0/docs/api/java/lang/reflect/Type.html">Type type, BeanViewDescriptorProvider provider, Converter<T> next) { |
||
51 | super(next); |
||
52 | this.provider = provider; |
||
53 | this.type = type; |
||
54 | } |
||
55 | |||
56 | @1.5.0/docs/api/java/lang/SuppressWarnings.html">SuppressWarnings("unchecked") |
||
57 | protected Class<? extends BeanView<T>> findViewFor(1.5.0/docs/api/java/lang/reflect/Type.html">Type type, |
||
58 | List<Class<? extends BeanView<?>>> views) { |
||
59 | for (Class<? extends BeanView<?>> v : views) { |
||
60 | 1.5.0/docs/api/java/lang/reflect/Type.html">Type searchedType = TypeUtil.lookupGenericType(BeanView.class, v); |
||
61 | searchedType = TypeUtil.expandType(searchedType, v); |
||
62 | searchedType = TypeUtil.typeOf(0, searchedType); |
||
63 | if (TypeUtil.match(type, searchedType, false)) { |
||
64 | return (Class<? extends BeanView<T>>) v; |
||
65 | } |
||
66 | } |
||
67 | return null; |
||
68 | } |
||
69 | |||
70 | public void serialize(T obj, ObjectWriter writer, 5+0%2Fdocs%2Fapi+Context">Context ctx) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
||
71 | boolean handled = false; |
||
72 | List<Class<? extends BeanView<?>>> views = ctx.views(); |
||
73 | if (views != null && views.size() > 0) { |
||
74 | Class<? extends BeanView<T>> viewClass = findViewFor(type, views); |
||
75 | if (viewClass != null) { |
||
76 | 1.5.0/docs/api/java/lang/reflect/Type.html">Type viewForType = TypeUtil.expandType(BeanView.class.getTypeParameters()[0], |
||
77 | viewClass); |
||
78 | @1.5.0/docs/api/java/lang/SuppressWarnings.html">SuppressWarnings("unchecked") |
||
79 | Class<T> viewForClass = (Class<T>) TypeUtil.getRawClass(viewForType); |
||
80 | BeanDescriptor<T> descriptor = provider |
||
81 | .provide(viewForClass, viewClass, ctx.genson); |
||
82 | descriptor.serialize(obj, writer, ctx); |
||
83 | handled = true; |
||
84 | } |
||
85 | } |
||
86 | if (!handled) wrapped.serialize(obj, writer, ctx); |
||
87 | } |
||
88 | |||
89 | public T deserialize(ObjectReader reader, 5+0%2Fdocs%2Fapi+Context">Context ctx) throws 1.5.0/docs/api/java/lang/Exception.html">Exception { |
||
90 | if (ctx.hasViews()) { |
||
91 | Class<? extends BeanView<T>> viewClass = findViewFor(type, ctx.views()); |
||
92 | if (viewClass != null) { |
||
93 | 1.5.0/docs/api/java/lang/reflect/Type.html">Type viewForType = TypeUtil.expandType(BeanView.class.getTypeParameters()[0], |
||
94 | viewClass); |
||
95 | @1.5.0/docs/api/java/lang/SuppressWarnings.html">SuppressWarnings("unchecked") |
||
96 | Class<T> viewForClass = (Class<T>) TypeUtil.getRawClass(viewForType); |
||
97 | BeanDescriptor<T> descriptor = provider |
||
98 | .provide(viewForClass, viewClass, ctx.genson); |
||
99 | return descriptor.deserialize(reader, ctx); |
||
100 | } |
||
101 | } |
||
102 | return wrapped.deserialize(reader, ctx); |
||
103 | } |
||
104 | } |