Skip to content

Commit 1bb3205

Browse files
committed
feat: 添加工具类 JsonUtil 和字符串与对象互转的方法
1 parent aea5ae6 commit 1bb3205

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package info.cadecode.simple.util;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.SerializationFeature;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.io.IOException;
13+
import java.text.SimpleDateFormat;
14+
15+
/**
16+
* @author Cade Li
17+
* @date 2021/7/15
18+
* @description: 基于 Jackson 的 json 工具类
19+
*/
20+
public class JsonUtil {
21+
22+
private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
23+
24+
private static final ObjectMapper objectMapper = new ObjectMapper();
25+
// 定义日期格式
26+
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
27+
28+
// 配置 Jackson
29+
static {
30+
// 列入对象的全部字段
31+
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
32+
// 取消默认转换 timestamps 形式
33+
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
34+
// 忽略空Bean转json的错误
35+
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
36+
// 统一所有的日期格式为以下的样式,即 yyyy-MM-dd HH:mm:ss
37+
objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
38+
// 忽略在 json 字符串中存在,但是在 java 对象中不存在对应属性的情况。防止错误
39+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
40+
}
41+
42+
/**
43+
* 转换对象到 json 串
44+
*
45+
* @param obj 需要转换的对象
46+
* @return json 字符串
47+
*/
48+
public static String objToStr(Object obj) {
49+
return objToStr(obj, false);
50+
}
51+
52+
/**
53+
* 转换对象到 json 串
54+
*
55+
* @param obj 需要转换的对象
56+
* @param isPretty 是否美化输出(换行)
57+
* @return json 字符串
58+
*/
59+
public static String objToStr(Object obj, boolean isPretty) {
60+
if (obj == null) {
61+
return null;
62+
}
63+
if (obj instanceof String) {
64+
return (String) obj;
65+
}
66+
try {
67+
if (!isPretty) {
68+
return objectMapper.writeValueAsString(obj);
69+
}
70+
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
71+
} catch (JsonProcessingException e) {
72+
log.error("Parse String to Object error", e);
73+
return null;
74+
}
75+
}
76+
77+
/**
78+
* 转换字符串到自定义对象
79+
*
80+
* @param str 需要转换的字符串
81+
* @param clazz 自定义对象的 class 对象
82+
* @return 自定义对象
83+
*/
84+
public static <T> T str2Obj(String str, Class<T> clazz) {
85+
if (StringUtil.isEmpty(str) || clazz == null) {
86+
return null;
87+
}
88+
if (clazz.equals(String.class)) {
89+
return (T) str;
90+
}
91+
try {
92+
return objectMapper.readValue(str, clazz);
93+
} catch (Exception e) {
94+
log.error("Parse String to Object error", e);
95+
return null;
96+
}
97+
}
98+
99+
/**
100+
* 转换字符串到集合对象,可以保留泛型
101+
*
102+
* @param str 需要转换的字符串
103+
* @param typeReference 自定义对象的 TypeReference 对象
104+
* @return 自定义对象
105+
*/
106+
public static <T> T str2Obj(String str, TypeReference<T> typeReference) {
107+
if (StringUtil.isEmpty(str) || typeReference == null) {
108+
return null;
109+
}
110+
if (typeReference.getType().equals(String.class)) {
111+
return (T) str;
112+
}
113+
try {
114+
return objectMapper.readValue(str, typeReference);
115+
} catch (IOException e) {
116+
log.error("Parse String to Object error", e);
117+
return null;
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)