This repository has been archived by the owner on Jan 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
GuavaMultimapDeserializer.java
183 lines (162 loc) · 7.15 KB
/
GuavaMultimapDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.fasterxml.jackson.datatype.guava.deser.multimap;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.MapLikeType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* @author mvolkhart
*/
public abstract class GuavaMultimapDeserializer<T extends Multimap<Object,
Object>> extends JsonDeserializer<T> implements ContextualDeserializer {
private static final List<String> METHOD_NAMES = ImmutableList.of("copyOf", "create");
private final MapLikeType type;
private final KeyDeserializer keyDeserializer;
private final TypeDeserializer elementTypeDeserializer;
private final JsonDeserializer<?> elementDeserializer;
/**
* Since we have to use a method to transform from a known multi-map type into actual one, we'll
* resolve method just once, use it. Note that if this is set to null, we can just construct a
* {@link com.google.common.collect.LinkedListMultimap} instance and be done with it.
*/
private final Method creatorMethod;
public GuavaMultimapDeserializer(MapLikeType type, KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) {
this(type, keyDeserializer, elementTypeDeserializer, elementDeserializer,
findTransformer(type.getRawClass()));
}
public GuavaMultimapDeserializer(MapLikeType type, KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer,
Method creatorMethod) {
this.type = type;
this.keyDeserializer = keyDeserializer;
this.elementTypeDeserializer = elementTypeDeserializer;
this.elementDeserializer = elementDeserializer;
this.creatorMethod = creatorMethod;
}
private static Method findTransformer(Class<?> rawType) {
// Very first thing: if it's a "standard multi-map type", can avoid copying
if (rawType == LinkedListMultimap.class || rawType == ListMultimap.class || rawType ==
Multimap.class) {
return null;
}
// First, check type itself for matching methods
for (String methodName : METHOD_NAMES) {
try {
Method m = rawType.getMethod(methodName, Multimap.class);
if (m != null) {
return m;
}
} catch (NoSuchMethodException e) {
}
// pass SecurityExceptions as-is:
// } catch (SecurityException e) { }
}
// If not working, possibly super types too (should we?)
for (String methodName : METHOD_NAMES) {
try {
Method m = rawType.getMethod(methodName, Multimap.class);
if (m != null) {
return m;
}
} catch (NoSuchMethodException e) {
}
// pass SecurityExceptions as-is:
// } catch (SecurityException e) { }
}
return null;
}
protected abstract T createMultimap();
/**
* We need to use this method to properly handle possible contextual variants of key and value
* deserializers, as well as type deserializers.
*/
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException {
KeyDeserializer kd = keyDeserializer;
if (kd == null) {
kd = ctxt.findKeyDeserializer(type.getKeyType(), property);
}
JsonDeserializer<?> ed = elementDeserializer;
if (ed == null) {
ed = ctxt.findContextualValueDeserializer(type.getContentType(), property);
}
// Type deserializer is slightly different; must be passed, but needs to become contextual:
TypeDeserializer etd = elementTypeDeserializer;
if (etd != null && property != null) {
etd = etd.forProperty(property);
}
return (_createContextual(type, kd, etd, ed, creatorMethod));
}
protected abstract JsonDeserializer<?> _createContextual(MapLikeType t,
KeyDeserializer kd, TypeDeserializer typeDeserializer,
JsonDeserializer<?> ed, Method method);
@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
T multimap = createMultimap();
expect(jp, JsonToken.START_OBJECT);
while (jp.nextToken() != JsonToken.END_OBJECT) {
final Object key;
if (keyDeserializer != null) {
key = keyDeserializer.deserializeKey(jp.getCurrentName(), ctxt);
} else {
key = jp.getCurrentName();
}
jp.nextToken();
expect(jp, JsonToken.START_ARRAY);
while (jp.nextToken() != JsonToken.END_ARRAY) {
final Object value;
if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
value = null;
} else if (elementTypeDeserializer != null) {
value = elementDeserializer.deserializeWithType(jp, ctxt,
elementTypeDeserializer);
} else {
value = elementDeserializer.deserialize(jp, ctxt);
}
multimap.put(key, value);
}
}
if (creatorMethod == null) {
return multimap;
}
try {
@SuppressWarnings("unchecked") T map = (T) creatorMethod.invoke(null, multimap);
return map;
} catch (InvocationTargetException e) {
throw new JsonMappingException("Could not map to " + type, _peel(e));
} catch (IllegalArgumentException e) {
throw new JsonMappingException("Could not map to " + type, _peel(e));
} catch (IllegalAccessException e) {
throw new JsonMappingException("Could not map to " + type, _peel(e));
}
}
private void expect(JsonParser jp, JsonToken token) throws IOException {
if (jp.getCurrentToken() != token) {
throw new JsonMappingException("Expecting " + token + ", found " + jp.getCurrentToken(),
jp.getCurrentLocation());
}
}
private Throwable _peel(Throwable t) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
}