JacksonUtil.java
3.65 KB
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
package com.sitech.ismp.util;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import java.lang.reflect.Type;
import java.util.*;
/**
*
* <p>
* Title: JacksonUtil
* </p>
* <p>
* Description: Jackson工具类
* </p>
* <p>
* Copyright: Copyright (c) 2013
* </p>
* <p>
* Company: SI-TECH
* </p>
*
* @author huojla
* @version 1.0
* @createtime 2013-5-30 下午1:13:29
*
*/
public class JacksonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// mapper.getDeserializationConfig().without(
// DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.getDeserializationConfig().set(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@SuppressWarnings("rawtypes")
public static class TypeReference<T extends Object> extends
org.codehaus.jackson.type.TypeReference {
public int compareTo(Object o) {
return 0;
}
public Type getTypeReference() {
return this.getType();
}
}
/**
*
* @Title: toJson
* @Description: 将对象转换为Json字符串
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-4-28 ����4:47:44
*/
public static String toJson(Object obj) {
try {
if (obj == null) {
return null;
} else {
return mapper.writeValueAsString(obj);
}
} catch (Exception e) {
throw new RuntimeException("转换Json异常!" + obj.getClass().getName(), e);
}
}
/**
*
* @Title: toObject
* @Description: 转换Json to 对象
* @param
* @return Object
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-4-28 ����4:54:25
*/
public static Object toObject(String json) {
return fromJson(json, Object.class);
}
/**
*
* @Title: fromJson
* @Description: 转换json to Java对象
* @param
* @return T
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-4-28 ����4:51:42
*/
public static <T> T fromJson(String json, Class<T> type) {
try {
if (json == null) {
return null;
} else {
return mapper.readValue(json, type);
}
} catch (Exception e) {
throw new RuntimeException(json + "解析Json异常!" + type.getName(), e);
}
}
/**
*
* @Title: fromJSON
* @Description: 转换复杂对象
* @param
* @return T
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-4-28 ����4:53:39
*/
public static <T> T fromJSON(String json, JacksonUtil.TypeReference<T> typeReference) {
try {
if (json == null) {
return null;
} else {
return (T) mapper.readValue(json, typeReference);
}
} catch (Exception ex) {
throw new RuntimeException(json + "解析Json异常!", ex);
}
}
/**
* @Title: JSONToMap
* @Description: 将json字符串转换成Map
* @param
* @return void
* @throws
* @author lipengpeng
* @version 1.0
* @createtime 2013-8-15 下午9:13:57
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> JSONToMap(String jsonstr) {
Map<String, Object> map = new HashMap<String, Object>();
List<LinkedHashMap<String, Object>> list = new ArrayList<LinkedHashMap<String, Object>>();
try {
list = mapper.readValue(jsonstr, List.class);
for (LinkedHashMap<String, Object> linkedHashMap : list) {
for (String key : linkedHashMap.keySet()) {
map.put(key, linkedHashMap.get(key));
}
}
} catch (Exception e) {
throw new RuntimeException(jsonstr + "解析Json异常!", e);
}
return map;
}
}