26
26
import com .fasterxml .jackson .core .JsonParser ;
27
27
import com .fasterxml .jackson .core .JsonProcessingException ;
28
28
import com .fasterxml .jackson .core .ObjectCodec ;
29
+ import com .fasterxml .jackson .databind .DatabindContext ;
29
30
import com .fasterxml .jackson .databind .DeserializationConfig ;
30
31
import com .fasterxml .jackson .databind .DeserializationContext ;
31
32
import com .fasterxml .jackson .databind .JavaType ;
46
47
import com .fasterxml .jackson .databind .jsontype .TypeSerializer ;
47
48
import com .fasterxml .jackson .databind .jsontype .impl .StdTypeResolverBuilder ;
48
49
import com .fasterxml .jackson .databind .type .TypeFactory ;
49
- import static org .junit .Assert .assertEquals ;
50
- import static org .junit .Assert .assertFalse ;
51
- import static org .junit .Assert .assertTrue ;
52
50
import org .junit .Before ;
53
51
import org .junit .Test ;
54
52
57
55
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
58
56
import org .springframework .beans .factory .support .RootBeanDefinition ;
59
57
58
+ import static org .junit .Assert .*;
59
+
60
60
/**
61
61
* Test class for {@link SpringHandlerInstantiatorTests}.
62
62
*
65
65
public class SpringHandlerInstantiatorTests {
66
66
67
67
private SpringHandlerInstantiator instantiator ;
68
+
68
69
private ObjectMapper objectMapper ;
69
70
71
+
70
72
@ Before
71
73
public void setup () {
72
74
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
@@ -78,6 +80,7 @@ public void setup() {
78
80
objectMapper = Jackson2ObjectMapperBuilder .json ().handlerInstantiator (instantiator ).build ();
79
81
}
80
82
83
+
81
84
@ Test
82
85
public void autowiredSerializer () throws JsonProcessingException {
83
86
User user = new User ("bob" );
@@ -113,6 +116,7 @@ public void applicationContextAwareTypeIdResolver() throws JsonProcessingExcepti
113
116
assertTrue (CustomTypeIdResolver .isAutowiredFiledInitialized );
114
117
}
115
118
119
+
116
120
public static class UserDeserializer extends JsonDeserializer <User > {
117
121
118
122
@ Autowired
@@ -124,9 +128,9 @@ public User deserialize(JsonParser jsonParser, DeserializationContext deserializ
124
128
JsonNode node = oc .readTree (jsonParser );
125
129
return new User (this .capitalizer .capitalize (node .get ("username" ).asText ()));
126
130
}
127
-
128
131
}
129
132
133
+
130
134
public static class UserSerializer extends JsonSerializer <User > {
131
135
132
136
@ Autowired
@@ -140,6 +144,7 @@ public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider
140
144
}
141
145
}
142
146
147
+
143
148
public static class UpperCaseKeyDeserializer extends KeyDeserializer {
144
149
145
150
@ Autowired
@@ -151,6 +156,7 @@ public Object deserializeKey(String key, DeserializationContext context) throws
151
156
}
152
157
}
153
158
159
+
154
160
public static class CustomTypeResolverBuilder extends StdTypeResolverBuilder {
155
161
156
162
@ Autowired
@@ -170,6 +176,7 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java
170
176
}
171
177
}
172
178
179
+
173
180
public static class CustomTypeIdResolver implements TypeIdResolver {
174
181
175
182
@ Autowired
@@ -178,7 +185,6 @@ public static class CustomTypeIdResolver implements TypeIdResolver {
178
185
public static boolean isAutowiredFiledInitialized = false ;
179
186
180
187
public CustomTypeIdResolver () {
181
-
182
188
}
183
189
184
190
@ Override
@@ -204,15 +210,20 @@ public String idFromValue(Object value) {
204
210
205
211
@ Override
206
212
public void init (JavaType type ) {
207
-
208
213
}
209
214
210
215
@ Override
211
216
public String idFromBaseType () {
212
217
return null ;
213
218
}
219
+
220
+ // New in Jackson 2.5
221
+ public JavaType typeFromId (DatabindContext context , String id ) {
222
+ return null ;
223
+ }
214
224
}
215
225
226
+
216
227
@ JsonDeserialize (using = UserDeserializer .class )
217
228
@ JsonSerialize (using = UserSerializer .class )
218
229
public static class User {
@@ -229,6 +240,7 @@ public User(String username) {
229
240
public String getUsername () { return this .username ; }
230
241
}
231
242
243
+
232
244
public static class SecurityRegistry {
233
245
234
246
@ JsonDeserialize (keyUsing = UpperCaseKeyDeserializer .class )
@@ -243,6 +255,7 @@ public Map<String, String> getCredentials() {
243
255
}
244
256
}
245
257
258
+
246
259
@ JsonTypeInfo (use = JsonTypeInfo .Id .CUSTOM , property = "type" )
247
260
@ JsonTypeResolver (CustomTypeResolverBuilder .class )
248
261
@ JsonTypeIdResolver (CustomTypeIdResolver .class )
@@ -255,18 +268,19 @@ public Group(String name) {
255
268
}
256
269
257
270
public Group () {
258
-
259
271
}
260
272
261
273
public String getType () {
262
274
return Group .class .getName ();
263
275
}
264
276
}
265
277
278
+
266
279
public static class Capitalizer {
267
280
268
281
public String capitalize (String text ) {
269
282
return text .toUpperCase ();
270
283
}
271
284
}
285
+
272
286
}
0 commit comments