-
Notifications
You must be signed in to change notification settings - Fork 7
/
JsonUtil.java
47 lines (38 loc) · 1.48 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
42
43
44
45
46
47
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.text.SimpleDateFormat;
public final class JsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
private JsonUtil() {}
public static final String DEFAULT_DATETIME_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
static {
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.configure(DeserializationConfig.Feature.AUTO_DETECT_FIELDS, true);
mapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATETIME_FORMAT_YYYY_MM_DD_HH_MM_SS));
}
public static String toJson(Object o) {
try {
return mapper.writeValueAsString(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("rawtypes")
public static <T> T fromJson(String json, TypeReference type) {
try {
return mapper.readValue(json, type);
} catch (Exception error) {
throw new RuntimeException(error);
}
}
public static <T> T fromJson(String json, Class<T> clazz) {
try {
return mapper.readValue(json, clazz);
} catch (Exception error) {
throw new RuntimeException(error);
}
}
}