-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathJsonUtil.java
More file actions
193 lines (180 loc) · 6.68 KB
/
JsonUtil.java
File metadata and controls
193 lines (180 loc) · 6.68 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package common.base.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
/**
* User: fee(1176610771@qq.com)
* Date: 2016-06-29
* Time: 18:21
* DESC:
*/
public final class JsonUtil {
private static ObjectMapper mapper = null;
static {
mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);//属性为 空(“”) 或者为 NULL 都不序列化
//解决kotlin中数据类非空类型但是json反序列化时为空抛异常的问题
mapper.registerModule(new KotlinModule.Builder().nullIsSameAsDefault(true).nullToEmptyMap(true).nullToEmptyCollection(true).build());
}
private JsonUtil() {
}
/**
* 将一个Java对象转换成Json字符串表示
* @param nodeName 表示该Java对象的节点名称,eg.: user:{"name":"jack","sex":1}; 其中user:即为nodeName
* @param curObject 支持Map,List
* @return 如果nodeName为null
* @throws IOException
*/
public static String convertObject2JsonStr(String nodeName, Object curObject) throws IOException {
if (curObject != null) {
if (curObject instanceof JSONObject) {//因为如果是JSONObject对象,会序列化失败 No serializer found for class org.json.JSONObject and no pro
return curObject.toString();
}
}
if (Util.isEmpty(nodeName)) {
return mapper.writeValueAsString(curObject);
}
ObjectNode rootNode = mapper.createObjectNode();
rootNode.putPOJO(nodeName, curObject);
return mapper.writeValueAsString(rootNode);
}
/**
* 将Object对象转为JSONObject 字符串
* @param toJsonObj
* @return
*/
public static String convertObj2JSonStr(Object toJsonObj) {
String jsonResultStr = "";
try {
jsonResultStr = converObject2JsonStr(toJsonObj);
} catch (IOException e) {
jsonResultStr = "";
e.printStackTrace();
}
return jsonResultStr;
}
/**
* 将一个Java对象转换成Json字符串
* @param curObject
* @return eg.: User ==> {"name":"jack","sex":1,"city":"sz"}
* @throws IOException
*/
public static String converObject2JsonStr(Object curObject) throws IOException {
return convertObject2JsonStr(null, curObject);
}
/***
* 将一个Java对象转换成JsonObject对象(这是无聊的节奏)
* @param curObject
* @return
* @throws IOException
* @throws JSONException
*/
public static JSONObject object2JsonObject(Object curObject) throws IOException, JSONException {
String object2JsonStr = converObject2JsonStr(curObject);
return new JSONObject(object2JsonStr);
}
/**
* 将Json字符串转换成Java对象(反序列化)
* @param jsonStr
* @param javaObjClass 目标Java对象的类型
* @param <T>
* @return
* @throws IOException
*/
public static <T> T jsonStr2Object(String jsonStr, Class<T> javaObjClass) throws IOException {
return mapper.readValue(jsonStr, javaObjClass);
}
public static <T> T jsonStr2Obj(String jsonStr, Class<T> javaObjClass) {
T javaObj = null;
try {
javaObj = mapper.readValue(jsonStr, javaObjClass);
} catch (Exception e) {
e.printStackTrace();
}
return javaObj;
}
/**
* json 数组字符串 转换成List集合
* @param jsonArrayStr
* @param listTypeReference
* @param <T> 集合中元素的对象类型
* @return
*/
public static <T> List<T> jsonArrayStr2ListObject(String jsonArrayStr, TypeReference<List<T>> listTypeReference) {
try {
return mapper.readValue(jsonArrayStr, listTypeReference);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 依List中元素的Java类型将 json 数组字符串,转换成List集合
* @param jsonArrayStr
* @param elementClass List中元素的Java类型
* @param <T>
* @return
*/
public static <T> List<T> jsonArrayStr2ListObject(String jsonArrayStr, Class<T> elementClass) {
JavaType resultJavaBeanType = mapper.getTypeFactory().constructParametricType(List.class, elementClass);
try {
return mapper.readValue(jsonArrayStr, resultJavaBeanType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据提供的 键值对信息,组装成一个JSONObject对象
* @param keys 键
* @param values 值
* @return JSONObject 对象 eg.: {"name":"feer","sex":"男", ...}
*/
public static JSONObject assembleJSONObj(String[] keys,Object... values) {
JSONObject paramJson = new JSONObject();
if (keys != null && values != null) {
int keyLen = Math.min(keys.length, values.length);
for (int i = 0; i < keyLen; i++) {
String curKey = keys[i];
Object curV = values[i];
try {
paramJson.put(curKey, curV);
} catch (JSONException e) {
CommonLog.e("info", "JsonUtil --> assembleJsonObj() put the key :" + curKey + " the value: " + curV + " occur :" + e);
}
}
}
return paramJson;
}
public static HashMap<String, Object> jsonStr2HashMap(String jsonStr) {
if (jsonStr != null && "".equals(jsonStr.trim())) {
try {
JavaType jvt = mapper.getTypeFactory().constructParametricType(HashMap.class,String.class,Object.class);
return mapper.readValue(jsonStr, jvt);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public static void jsonPutKVIgnoreException(JSONObject jsonObj, String theKey, Object theValue) {
if (jsonObj != null) {
try {
// jsonObj.putOpt(theKey, theValue);//要求 key value 都不为空
jsonObj.put(theKey, theValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}