-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonUtil.java
41 lines (33 loc) · 1.06 KB
/
JsonUtil.java
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
package zw.co.getcash.utilities;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.context.annotation.Bean;
/**
* @author autowired
* Date 4/9/17
*/
public class JsonUtil {
private final static ObjectMapper objectMapper = new ObjectMapper();
private final static Logger logger = Logger.getLogger(JsonUtil.class);
static {
objectMapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
}
public static <T> T fromJson(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (Exception e) {
e.printStackTrace();
logger.error("Json error:" + e.getMessage());
return null;
}
}
public static JSONObject toJson(Object object) {
try {
return new JSONObject(objectMapper.writeValueAsString(object));
} catch (Exception e) {
return null;
}
}
}