1
|
package org.bgbm.biovel.drf.utils;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.apache.http.ParseException;
|
7
|
import org.bgbm.biovel.drf.checklist.DRFChecklistException;
|
8
|
import org.codehaus.jackson.type.TypeReference;
|
9
|
import org.json.simple.JSONArray;
|
10
|
import org.json.simple.JSONObject;
|
11
|
import org.json.simple.parser.JSONParser;
|
12
|
|
13
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
14
|
import com.fasterxml.jackson.core.JsonGenerationException;
|
15
|
import com.fasterxml.jackson.core.JsonParseException;
|
16
|
|
17
|
|
18
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
19
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
20
|
|
21
|
public class JSONUtils {
|
22
|
|
23
|
public static JSONObject parseJsonToObject(String json) throws DRFChecklistException {
|
24
|
JSONParser parser = new JSONParser();
|
25
|
JSONObject obj;
|
26
|
try {
|
27
|
obj = (JSONObject)parser.parse(json);
|
28
|
} catch (ParseException e) {
|
29
|
// TODO Auto-generated catch block
|
30
|
e.printStackTrace();
|
31
|
throw new DRFChecklistException(e);
|
32
|
} catch (org.json.simple.parser.ParseException e) {
|
33
|
// TODO Auto-generated catch block
|
34
|
e.printStackTrace();
|
35
|
throw new DRFChecklistException(e);
|
36
|
}
|
37
|
return obj;
|
38
|
}
|
39
|
|
40
|
public static JSONArray parseJsonToArray(String json) throws DRFChecklistException {
|
41
|
JSONParser parser = new JSONParser();
|
42
|
JSONArray obj;
|
43
|
try {
|
44
|
obj = (JSONArray)parser.parse(json);
|
45
|
} catch (ParseException e) {
|
46
|
// TODO Auto-generated catch block
|
47
|
e.printStackTrace();
|
48
|
throw new DRFChecklistException(e);
|
49
|
} catch (org.json.simple.parser.ParseException e) {
|
50
|
// TODO Auto-generated catch block
|
51
|
e.printStackTrace();
|
52
|
throw new DRFChecklistException(e);
|
53
|
}
|
54
|
return obj;
|
55
|
}
|
56
|
|
57
|
public static <T> T convertJsonToObject(String json, Class<T> clazz) throws DRFChecklistException {
|
58
|
|
59
|
ObjectMapper mapper = new ObjectMapper();
|
60
|
mapper.setSerializationInclusion(Include.NON_NULL);
|
61
|
try {
|
62
|
return mapper.readValue(json, clazz);
|
63
|
} catch (JsonParseException e) {
|
64
|
// TODO Auto-generated catch block
|
65
|
e.printStackTrace();
|
66
|
throw new DRFChecklistException(e);
|
67
|
} catch (JsonMappingException e) {
|
68
|
// TODO Auto-generated catch block
|
69
|
e.printStackTrace();
|
70
|
throw new DRFChecklistException(e);
|
71
|
} catch (IOException e) {
|
72
|
// TODO Auto-generated catch block
|
73
|
e.printStackTrace();
|
74
|
throw new DRFChecklistException(e);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
|
79
|
public static String convertObjectToJson(Object obj) throws DRFChecklistException {
|
80
|
ObjectMapper mapper = new ObjectMapper();
|
81
|
mapper.setSerializationInclusion(Include.NON_NULL);
|
82
|
try {
|
83
|
return mapper.writeValueAsString(obj);
|
84
|
} catch (JsonGenerationException e) {
|
85
|
// TODO Auto-generated catch block
|
86
|
e.printStackTrace();
|
87
|
throw new DRFChecklistException(e);
|
88
|
} catch (JsonMappingException e) {
|
89
|
// TODO Auto-generated catch block
|
90
|
e.printStackTrace();
|
91
|
throw new DRFChecklistException(e);
|
92
|
} catch (IOException e) {
|
93
|
// TODO Auto-generated catch block
|
94
|
e.printStackTrace();
|
95
|
throw new DRFChecklistException(e);
|
96
|
}
|
97
|
}
|
98
|
}
|