1
2
3
4
5 package gboat2.base.core.util;
6
7 import gboat2.base.bridge.GboatAppConstants;
8 import gboat2.base.bridge.util.FileUtil;
9 import gboat2.base.core.util.json.GboatJsonDateValueProcessor;
10
11 import java.lang.reflect.Method;
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20
21 import net.sf.ezmorph.object.DateMorpher;
22 import net.sf.json.JSONArray;
23 import net.sf.json.JSONObject;
24 import net.sf.json.JsonConfig;
25 import net.sf.json.util.JSONUtils;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36
37 public class JsonUtil {
38
39 private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
40
41
42
43
44
45
46
47 public static JSONObject loadFileAsJSONObject(URL url, String charset) {
48 return JSONObject.fromObject(FileUtil.loadAsString(url, charset));
49 }
50
51
52
53
54
55
56
57
58
59
60
61 public static JSONObject loadFileAsJSONObject(URL url) {
62 return loadFileAsJSONObject(url, GboatAppConstants.ENCODING_UTF8);
63 }
64
65
66
67
68
69
70
71 public static JSONArray loadFileAsJSONArray(URL url, String charset) {
72 return JSONArray.fromObject(FileUtil.loadAsString(url, charset));
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public static JSONArray loadFileAsJSONArray(URL url) {
86 return loadFileAsJSONArray(url, GboatAppConstants.ENCODING_UTF8);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public static <T> T toBean(JSONObject jsonObj, Class T, String dateFormat) {
103 if (StringUtils.isEmpty(dateFormat)) {
104 dateFormat = "yyyy-MM-dd";
105 }
106
107 JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { dateFormat }), false);
108 processNullValueOfJsonObj(jsonObj);
109 T ret = (T) JSONObject.toBean(jsonObj, T);
110 return ret;
111 }
112
113
114
115
116
117
118
119 @SuppressWarnings("unchecked")
120 private static void processNullValueOfJsonObj(JSONObject jsonObj) {
121 if (jsonObj == null) {
122 return;
123 }
124 Set set = jsonObj.entrySet();
125 Iterator iter = set.iterator();
126 while (iter.hasNext()) {
127 Map.Entry enter = (Entry) iter.next();
128 Object v = enter.getValue();
129 if (v == null || (v instanceof String && StringUtils.isEmpty((String) v))) {
130 iter.remove();
131 }
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 public static <T> List<T> parseJSONArray(String jsonArrayStr, Class T , String dateFormat) throws Exception {
147 try {
148 if (jsonArrayStr == null) {
149 return null;
150 }
151 JSONArray json = JSONArray.fromObject(jsonArrayStr);
152 Iterator<JSONObject> iterator = json.iterator();
153 List returnList = new ArrayList();
154 while (iterator.hasNext()) {
155 JSONObject jsonObject = iterator.next();
156 Object returnObject = null;
157 returnObject = toBean(jsonObject, T, dateFormat);
158
159 if (returnObject != null) {
160 returnList.add(returnObject);
161 }
162 }
163 return returnList;
164 } catch (Throwable e) {
165 logger.error("parseJSONArray Exception!!!jsonArrayStr:{ " + jsonArrayStr + " }", e);
166 throw new Exception("parseJSONArray Exception!!!jsonArrayStr:{ " + jsonArrayStr + " }");
167 }
168 }
169
170
171 public static <T> List<T> parseJSONArray(String jsonArrayStr, Class T) throws Exception {
172 try {
173 if (jsonArrayStr == null) {
174 return null;
175 }
176 JsonConfig jsonConfig = JsonConfigUtil.getJsonConfig();
177 jsonConfig.registerJsonValueProcessor(Date.class, new GboatJsonDateValueProcessor("yyyy-MM-dd"));
178
179 JSONArray json = JSONArray.fromObject(jsonArrayStr, jsonConfig);
180 Iterator<JSONObject> iterator = json.iterator();
181 List returnList = new ArrayList();
182 while (iterator.hasNext()) {
183 JSONObject jsonObject = iterator.next();
184
185
186 Object returnObject = T.newInstance();
187 Method[] methodes = T.getMethods();
188 if (methodes == null) {
189 return null;
190 }
191
192 for (Method method : methodes) {
193
194 String methodName = null;
195 if (method.getName().indexOf("set") == 0) {
196
197 methodName = method.getName().replaceFirst("set", "");
198 String frist = methodName.substring(0, 1).toLowerCase();
199 methodName = frist + methodName.substring(1);
200
201 } else if (method.getName().indexOf("is") == 0) {
202
203 methodName = method.getName().replaceFirst("is", "");
204 String frist = methodName.substring(0, 1).toLowerCase();
205 methodName = frist + methodName.substring(1);
206
207 }
208 if (methodName != null && methodName.length() > 0) {
209
210 Object valueObject = jsonObject.get(methodName);
211 Class[] paramentClassList = method.getParameterTypes();
212
213 if (paramentClassList != null && paramentClassList.length == 1 && valueObject != null
214 && paramentClassList[0].equals(valueObject.getClass())) {
215
216 method.invoke(returnObject, valueObject);
217
218 }
219 }
220 }
221
222 if (returnObject != null) {
223 returnList.add(returnObject);
224 }
225
226 }
227 return returnList;
228 } catch (Throwable e) {
229 logger.error("parseJSONArray Exception!!!jsonArrayStr:{ " + jsonArrayStr + " }", e);
230 throw new Exception("parseJSONArray Exception!!!jsonArrayStr:{ " + jsonArrayStr + " }");
231 }
232 }
233
234
235
236 public static <T> T parse(JSONObject jsonObject, Class T) throws Exception {
237
238 Object returnObject = T.newInstance();
239 Method[] methodes = T.getMethods();
240 if (methodes == null) {
241 return null;
242 }
243
244 for (Method method : methodes) {
245
246 String methodName = null;
247 if (method.getName().indexOf("set") == 0) {
248
249 methodName = method.getName().replaceFirst("set", "");
250 String frist = methodName.substring(0, 1).toLowerCase();
251 methodName = frist + methodName.substring(1);
252
253 } else if (method.getName().indexOf("is") == 0) {
254
255 methodName = method.getName().replaceFirst("is", "");
256 String frist = methodName.substring(0, 1).toLowerCase();
257 methodName = frist + methodName.substring(1);
258
259 }
260 if (methodName != null && methodName.length() > 0) {
261
262 Object valueObject = jsonObject.get(methodName);
263 Class[] paramentClassList = method.getParameterTypes();
264
265 if (paramentClassList != null && paramentClassList.length == 1 && valueObject != null
266 && paramentClassList[0].equals(valueObject.getClass())) {
267
268 method.invoke(returnObject, valueObject);
269
270 }
271 }
272 }
273
274 return (T) returnObject;
275 }
276
277
278
279 public static JSONObject translateJSONOjbject(String dataIndexObj, JSONObject dataJObj) {
280 JSONObject targetJSONObject = new JSONObject();
281 if (!StringUtils.isEmpty(dataIndexObj)) {
282 Object elementObj = dataJObj.get(dataIndexObj);
283 if (elementObj instanceof String) {
284 String dataObj = (String) dataJObj.get(dataIndexObj);
285 targetJSONObject.accumulate(dataIndexObj, dataObj);
286 } else if (elementObj instanceof Enum<?>) {
287
288 dataJObj.get(dataIndexObj);
289 } else {
290 Object value = dataJObj.get(dataIndexObj);
291 if (value != null) {
292 if (value instanceof JSONObject) {
293 JSONObject dataObj = (JSONObject) value;
294 targetJSONObject.accumulate(dataIndexObj, dataObj);
295 } else if (value.getClass().getPackage().getName().startsWith("java.lang")) {
296 targetJSONObject.accumulate(dataIndexObj, value.toString());
297 } else if (value instanceof List<?> || value instanceof Object[]) {
298 JSONArray dataObj = JSONArray.fromObject(value);
299 targetJSONObject.accumulate(dataIndexObj, dataObj);
300 } else {
301 JSONObject dataObj = JSONObject.fromObject(value);
302 targetJSONObject.accumulate(dataIndexObj, dataObj);
303 }
304 }
305 }
306 }
307 return targetJSONObject;
308 }
309
310
311
312
313
314
315
316
317 public static String string2Json(String s) {
318
319 if(StringUtils.isEmpty(s))
320 return "";
321
322 StringBuffer sb = new StringBuffer();
323 for (int i = 0; i < s.length(); i++) {
324 char c = s.charAt(i);
325 switch (c) {
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 case '\n':
342 sb.append("\\n");
343 break;
344 case '\r':
345 sb.append("\\r");
346 break;
347 case '\t':
348 sb.append("\\t");
349 break;
350 default:
351 sb.append(c);
352 }
353 }
354 return sb.toString();
355 }
356 }