Skip to content

Commit f4d39db

Browse files
committed
refactor: ♻️ 优化JsonUtils 实现
1 parent 7b868bb commit f4d39db

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

bubble-core/src/main/java/cn/fxbin/bubble/core/util/JsonUtils.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import cn.fxbin.bubble.core.exception.UtilException;
44
import cn.fxbin.bubble.core.module.JacksonHolder;
5+
import cn.hutool.json.JSONUtil;
56
import com.fasterxml.jackson.core.JsonProcessingException;
6-
import com.fasterxml.jackson.databind.ObjectMapper;
77
import lombok.experimental.UtilityClass;
88

99
import java.io.IOException;
@@ -16,7 +16,7 @@
1616
* @since 2020/3/20 17:28
1717
*/
1818
@UtilityClass
19-
public class JsonUtils {
19+
public class JsonUtils extends JSONUtil {
2020

2121

2222
/**
@@ -28,7 +28,13 @@ public class JsonUtils {
2828
*/
2929
public String toJson(Object object) {
3030
try {
31-
return getInstance().writeValueAsString(object);
31+
if (ObjectUtils.isEmpty(object)) {
32+
return null;
33+
}
34+
if (object instanceof CharSequence) {
35+
return StringUtils.utf8Str(object);
36+
}
37+
return JacksonHolder.INSTANCE.writeValueAsString(object);
3238
} catch (JsonProcessingException e) {
3339
throw new UtilException(e);
3440
}
@@ -44,7 +50,7 @@ public String toJson(Object object) {
4450
*/
4551
public boolean isJsonString(String jsonString) {
4652
try {
47-
getInstance().readTree(jsonString);
53+
JacksonHolder.INSTANCE.readTree(jsonString);
4854
return true;
4955
} catch (IOException e) {
5056
return false;
@@ -61,7 +67,7 @@ public boolean isJsonString(String jsonString) {
6167
*/
6268
public boolean isJsonSerialize(Object object) {
6369
try {
64-
getInstance().writeValueAsBytes(object);
70+
JacksonHolder.INSTANCE.writeValueAsBytes(object);
6571
return true;
6672
} catch (JsonProcessingException e) {
6773
return false;
@@ -79,22 +85,10 @@ public boolean isJsonSerialize(Object object) {
7985
*/
8086
public <T> T parse(String jsonString, Class<T> requiredType) {
8187
try {
82-
return getInstance().readValue(jsonString, requiredType);
88+
return JacksonHolder.INSTANCE.readValue(jsonString, requiredType);
8389
} catch (IOException e) {
8490
throw new UtilException(e);
8591
}
8692
}
8793

88-
89-
/**
90-
* getInstance 获取jackson实例
91-
*
92-
* @since 2020/3/20 17:30
93-
* @return com.fasterxml.jackson.databind.ObjectMapper
94-
*/
95-
private ObjectMapper getInstance() {
96-
return JacksonHolder.INSTANCE;
97-
}
98-
99-
10094
}

0 commit comments

Comments
 (0)