提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.core.util; |
H |
2 |
|
|
3 |
import com.fasterxml.jackson.core.JsonGenerationException; |
|
4 |
import com.fasterxml.jackson.core.JsonParseException; |
|
5 |
import com.fasterxml.jackson.databind.JavaType; |
|
6 |
import com.fasterxml.jackson.databind.JsonMappingException; |
|
7 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
|
|
11 |
import java.io.IOException; |
|
12 |
|
|
13 |
/** |
|
14 |
* Jackson util |
|
15 |
* |
|
16 |
* 1、obj need private and set/get; |
|
17 |
* 2、do not support inner class; |
|
18 |
* |
|
19 |
* @author xuxueli 2015-9-25 18:02:56 |
|
20 |
*/ |
|
21 |
public class JacksonUtil { |
|
22 |
private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class); |
|
23 |
|
|
24 |
private final static ObjectMapper objectMapper = new ObjectMapper(); |
|
25 |
public static ObjectMapper getInstance() { |
|
26 |
return objectMapper; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* bean、array、List、Map --> json |
|
31 |
* |
|
32 |
* @param obj |
|
33 |
* @return json string |
|
34 |
* @throws Exception |
|
35 |
*/ |
|
36 |
public static String writeValueAsString(Object obj) { |
|
37 |
try { |
|
38 |
return getInstance().writeValueAsString(obj); |
|
39 |
} catch (JsonGenerationException e) { |
|
40 |
logger.error(e.getMessage(), e); |
|
41 |
} catch (JsonMappingException e) { |
|
42 |
logger.error(e.getMessage(), e); |
|
43 |
} catch (IOException e) { |
|
44 |
logger.error(e.getMessage(), e); |
|
45 |
} |
|
46 |
return null; |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* string --> bean、Map、List(array) |
|
51 |
* |
|
52 |
* @param jsonStr |
|
53 |
* @param clazz |
|
54 |
* @return obj |
|
55 |
* @throws Exception |
|
56 |
*/ |
|
57 |
public static <T> T readValue(String jsonStr, Class<T> clazz) { |
|
58 |
try { |
|
59 |
return getInstance().readValue(jsonStr, clazz); |
|
60 |
} catch (JsonParseException e) { |
|
61 |
logger.error(e.getMessage(), e); |
|
62 |
} catch (JsonMappingException e) { |
|
63 |
logger.error(e.getMessage(), e); |
|
64 |
} catch (IOException e) { |
|
65 |
logger.error(e.getMessage(), e); |
|
66 |
} |
|
67 |
return null; |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* string --> List<Bean>... |
|
72 |
* |
|
73 |
* @param jsonStr |
|
74 |
* @param parametrized |
|
75 |
* @param parameterClasses |
|
76 |
* @param <T> |
|
77 |
* @return |
|
78 |
*/ |
|
79 |
public static <T> T readValue(String jsonStr, Class<?> parametrized, Class<?>... parameterClasses) { |
|
80 |
try { |
|
81 |
JavaType javaType = getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses); |
|
82 |
return getInstance().readValue(jsonStr, javaType); |
|
83 |
} catch (JsonParseException e) { |
|
84 |
logger.error(e.getMessage(), e); |
|
85 |
} catch (JsonMappingException e) { |
|
86 |
logger.error(e.getMessage(), e); |
|
87 |
} catch (IOException e) { |
|
88 |
logger.error(e.getMessage(), e); |
|
89 |
} |
|
90 |
return null; |
|
91 |
} |
|
92 |
} |