使用 lombok 优化工具类

1。添加 @UtilityClass, 作用私有空参构造函数
2。使用 @SneakyThrows 替代手动转换为 RuntimeException
This commit is contained in:
leosanqing 2022-02-09 09:36:15 +08:00
parent fd4adf2cea
commit 855bb214be

View File

@ -2,13 +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.SneakyThrows;
import lombok.experimental.UtilityClass; 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;
@ -20,6 +20,7 @@ import java.util.List;
* @author 芋道源码 * @author 芋道源码
*/ */
@UtilityClass @UtilityClass
@Slf4j
public class JsonUtils { public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper(); private static ObjectMapper objectMapper = new ObjectMapper();
@ -49,47 +50,72 @@ public class JsonUtils {
return objectMapper.writeValueAsBytes(object); return objectMapper.writeValueAsBytes(object);
} }
@SneakyThrows
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 {
return objectMapper.readValue(text, clazz); return objectMapper.readValue(text, clazz);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
} }
@SneakyThrows
public static <T> T parseObject(byte[] bytes, Class<T> clazz) { public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
if (ArrayUtil.isEmpty(bytes)) { if (ArrayUtil.isEmpty(bytes)) {
return null; return null;
} }
try {
return objectMapper.readValue(bytes, clazz); return objectMapper.readValue(bytes, clazz);
} catch (IOException e) {
log.error("json parse err,json:{}", bytes, e);
throw new RuntimeException(e);
}
} }
@SneakyThrows
public static <T> T parseObject(String text, TypeReference<T> typeReference) { public static <T> T parseObject(String text, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(text, typeReference); return objectMapper.readValue(text, typeReference);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
} }
@SneakyThrows
public static <T> List<T> parseArray(String text, Class<T> clazz) { public static <T> List<T> parseArray(String text, Class<T> clazz) {
if (StrUtil.isEmpty(text)) { if (StrUtil.isEmpty(text)) {
return new ArrayList<>(); return new ArrayList<>();
} }
try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
} }
// TODO @Li和上面的风格保持一致哈parseTree // TODO @Li和上面的风格保持一致哈parseTree
@SneakyThrows
public static JsonNode readTree(String text) { public static JsonNode readTree(String text) {
try {
return objectMapper.readTree(text); return objectMapper.readTree(text);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
} }
@SneakyThrows
public static JsonNode readTree(byte[] text) { public static JsonNode readTree(byte[] text) {
try {
return objectMapper.readTree(text); return objectMapper.readTree(text);
} catch (IOException e) {
log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e);
}
} }
} }