From fd4adf2cea7448f17326fd38380916e765742e08 Mon Sep 17 00:00:00 2001 From: leosanqing Date: Tue, 8 Feb 2022 19:25:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20lombok=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=B7=A5=E5=85=B7=E7=B1=BB=201=E3=80=82=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20@UtilityClass,=20=E4=BD=9C=E7=94=A8=E7=A7=81?= =?UTF-8?q?=E6=9C=89=E7=A9=BA=E5=8F=82=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0?= =?UTF-8?q?=202=E3=80=82=E4=BD=BF=E7=94=A8=20@SneakyThrows=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=E6=89=8B=E5=8A=A8=E8=BD=AC=E6=8D=A2=E4=B8=BA=20Runtim?= =?UTF-8?q?eException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/common/util/json/JsonUtils.java | 62 +++++++------------ 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java index be5b66a04..dd29b3759 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java @@ -7,6 +7,8 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import lombok.SneakyThrows; +import lombok.experimental.UtilityClass; import java.io.IOException; import java.util.ArrayList; @@ -17,6 +19,7 @@ import java.util.List; * * @author 芋道源码 */ +@UtilityClass public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); @@ -36,78 +39,57 @@ public class JsonUtils { JsonUtils.objectMapper = objectMapper; } + @SneakyThrows public static String toJsonString(Object object) { - try { - return objectMapper.writeValueAsString(object); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + return objectMapper.writeValueAsString(object); } + @SneakyThrows public static byte[] toJsonByte(Object object) { - try { - return objectMapper.writeValueAsBytes(object); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } + return objectMapper.writeValueAsBytes(object); } + @SneakyThrows public static T parseObject(String text, Class clazz) { if (StrUtil.isEmpty(text)) { return null; } - try { - return objectMapper.readValue(text, clazz); - } catch (IOException e) { - throw new RuntimeException(e); - } + + return objectMapper.readValue(text, clazz); } + @SneakyThrows public static T parseObject(byte[] bytes, Class clazz) { if (ArrayUtil.isEmpty(bytes)) { return null; } - try { - return objectMapper.readValue(bytes, clazz); - } catch (IOException e) { - throw new RuntimeException(e); - } + + return objectMapper.readValue(bytes, clazz); } + @SneakyThrows public static T parseObject(String text, TypeReference typeReference) { - try { - return objectMapper.readValue(text, typeReference); - } catch (IOException e) { - throw new RuntimeException(e); - } + return objectMapper.readValue(text, typeReference); } + @SneakyThrows public static List parseArray(String text, Class clazz) { if (StrUtil.isEmpty(text)) { return new ArrayList<>(); } - try { - return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); - } catch (IOException e) { - throw new RuntimeException(e); - } + + return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); } // TODO @Li:和上面的风格保持一致哈。parseTree + @SneakyThrows public static JsonNode readTree(String text) { - try { - return objectMapper.readTree(text); - } catch (IOException e) { - throw new RuntimeException(e); - } + return objectMapper.readTree(text); } + @SneakyThrows public static JsonNode readTree(byte[] text) { - try { - return objectMapper.readTree(text); - } catch (IOException e) { - throw new RuntimeException(e); - } + return objectMapper.readTree(text); } } From 855bb214be3501303d5a35fe051749541c940f86 Mon Sep 17 00:00:00 2001 From: leosanqing Date: Wed, 9 Feb 2022 09:36:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20lombok=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=B7=A5=E5=85=B7=E7=B1=BB=201=E3=80=82=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20@UtilityClass,=20=E4=BD=9C=E7=94=A8=E7=A7=81?= =?UTF-8?q?=E6=9C=89=E7=A9=BA=E5=8F=82=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0?= =?UTF-8?q?=202=E3=80=82=E4=BD=BF=E7=94=A8=20@SneakyThrows=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=E6=89=8B=E5=8A=A8=E8=BD=AC=E6=8D=A2=E4=B8=BA=20Runtim?= =?UTF-8?q?eException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/common/util/json/JsonUtils.java | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java index dd29b3759..35bac965c 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java @@ -2,13 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import lombok.SneakyThrows; import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; import java.io.IOException; import java.util.ArrayList; @@ -20,6 +20,7 @@ import java.util.List; * @author 芋道源码 */ @UtilityClass +@Slf4j public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); @@ -49,47 +50,72 @@ public class JsonUtils { return objectMapper.writeValueAsBytes(object); } - @SneakyThrows + public static T parseObject(String text, Class clazz) { if (StrUtil.isEmpty(text)) { return null; } - return objectMapper.readValue(text, clazz); + try { + return objectMapper.readValue(text, clazz); + } catch (IOException e) { + log.error("json parse err,json:{}", text, e); + throw new RuntimeException(e); + } } - @SneakyThrows public static T parseObject(byte[] bytes, Class clazz) { if (ArrayUtil.isEmpty(bytes)) { return null; } - return objectMapper.readValue(bytes, clazz); + try { + return objectMapper.readValue(bytes, clazz); + } catch (IOException e) { + log.error("json parse err,json:{}", bytes, e); + throw new RuntimeException(e); + } } - @SneakyThrows public static T parseObject(String text, TypeReference typeReference) { - return objectMapper.readValue(text, typeReference); + try { + return objectMapper.readValue(text, typeReference); + } catch (IOException e) { + log.error("json parse err,json:{}", text, e); + throw new RuntimeException(e); + } } - @SneakyThrows public static List parseArray(String text, Class clazz) { if (StrUtil.isEmpty(text)) { return new ArrayList<>(); } - return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); + try { + 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 - @SneakyThrows public static JsonNode readTree(String text) { - return objectMapper.readTree(text); + try { + 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) { - return objectMapper.readTree(text); + try { + return objectMapper.readTree(text); + } catch (IOException e) { + log.error("json parse err,json:{}", text, e); + throw new RuntimeException(e); + } } }