This commit is contained in:
YunaiV 2022-02-10 00:08:04 +08:00
commit 55d8977108

View File

@ -2,11 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,6 +19,8 @@ import java.util.List;
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@UtilityClass
@Slf4j
public class JsonUtils { public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper(); private static ObjectMapper objectMapper = new ObjectMapper();
@ -36,29 +40,26 @@ public class JsonUtils {
JsonUtils.objectMapper = objectMapper; JsonUtils.objectMapper = objectMapper;
} }
@SneakyThrows
public static String toJsonString(Object object) { public static String toJsonString(Object object) {
try { return objectMapper.writeValueAsString(object);
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
} }
@SneakyThrows
public static byte[] toJsonByte(Object object) { public static byte[] toJsonByte(Object object) {
try { return objectMapper.writeValueAsBytes(object);
return objectMapper.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
} }
public static <T> T parseObject(String text, Class<T> clazz) { public static <T> T parseObject(String text, Class<T> clazz) {
if (StrUtil.isEmpty(text)) { if (StrUtil.isEmpty(text)) {
return null; return null;
} }
try { try {
return objectMapper.readValue(text, clazz); return objectMapper.readValue(text, clazz);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -67,9 +68,11 @@ public class JsonUtils {
if (ArrayUtil.isEmpty(bytes)) { if (ArrayUtil.isEmpty(bytes)) {
return null; return null;
} }
try { try {
return objectMapper.readValue(bytes, clazz); return objectMapper.readValue(bytes, clazz);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", bytes, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -78,6 +81,7 @@ public class JsonUtils {
try { try {
return objectMapper.readValue(text, typeReference); return objectMapper.readValue(text, typeReference);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -86,9 +90,11 @@ public class JsonUtils {
if (StrUtil.isEmpty(text)) { if (StrUtil.isEmpty(text)) {
return new ArrayList<>(); return new ArrayList<>();
} }
try { try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -98,6 +104,7 @@ public class JsonUtils {
try { try {
return objectMapper.readTree(text); return objectMapper.readTree(text);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -106,6 +113,7 @@ public class JsonUtils {
try { try {
return objectMapper.readTree(text); return objectMapper.readTree(text);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }