替换带代码里所有 fastjson 的内容,统一使用 jackson
不考虑使用 gson 的原因,是基本停止了维护
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package cn.iocoder.dashboard.util.json;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JSON 工具类
|
||||
@@ -9,16 +12,35 @@ import com.alibaba.fastjson.JSON;
|
||||
*/
|
||||
public class JSONUtils {
|
||||
|
||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
// TODO 芋艿,需要将 Spring 的设置下进来
|
||||
public static void setObjectMapper(ObjectMapper objectMapper) {
|
||||
JSONUtils.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public static String toJSONString(Object object) {
|
||||
return JSON.toJSONString(object);
|
||||
try {
|
||||
return objectMapper.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T parseObject(String text, Class<T> clazz) {
|
||||
return JSON.parseObject(text, clazz);
|
||||
try {
|
||||
return objectMapper.readValue(text, clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
|
||||
return JSON.parseObject(bytes, clazz);
|
||||
try {
|
||||
return objectMapper.readValue(bytes, clazz);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import cn.iocoder.dashboard.util.json.JSONUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
@@ -28,7 +28,7 @@ public class ServletUtils {
|
||||
*/
|
||||
@SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE,否则会乱码
|
||||
public static void writeJSON(HttpServletResponse response, Object object) {
|
||||
String content = JSON.toJSONString(object);
|
||||
String content = JSONUtils.toJSONString(object);
|
||||
ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user