From f9d9eb2d70458839d292a5d34d18ddc79888f80e Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 12 一月 2023 15:11:56 +0800 Subject: [PATCH] fix 修复 新版本 Redisson 存在与 boot 2.X 的兼容性问题 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java | 66 +++++++++++++++++++++----------- 1 files changed, 43 insertions(+), 23 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java index 7246b0a..e5c471e 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java @@ -1,9 +1,12 @@ package com.ruoyi.common.utils; +import cn.hutool.core.lang.Dict; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjectUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -11,7 +14,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Map; /** * JSON 宸ュ叿绫� @@ -21,14 +23,18 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class JsonUtils { - private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class); + private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); + + public static ObjectMapper getObjectMapper() { + return OBJECT_MAPPER; + } public static String toJsonString(Object object) { - if (StringUtils.isNull(object)) { - return null; - } + if (ObjectUtil.isNull(object)) { + return null; + } try { - return objectMapper.writeValueAsString(object); + return OBJECT_MAPPER.writeValueAsString(object); } catch (JsonProcessingException e) { throw new RuntimeException(e); } @@ -39,7 +45,7 @@ return null; } try { - return objectMapper.readValue(text, clazz); + return OBJECT_MAPPER.readValue(text, clazz); } catch (IOException e) { throw new RuntimeException(e); } @@ -50,40 +56,54 @@ return null; } try { - return objectMapper.readValue(bytes, clazz); + return OBJECT_MAPPER.readValue(bytes, clazz); } catch (IOException e) { throw new RuntimeException(e); } } public static <T> T parseObject(String text, TypeReference<T> typeReference) { - if (StringUtils.isBlank(text)) { - return null; - } + if (StringUtils.isBlank(text)) { + return null; + } try { - return objectMapper.readValue(text, typeReference); + return OBJECT_MAPPER.readValue(text, typeReference); } catch (IOException e) { throw new RuntimeException(e); } } - public static <T> Map<String, T> parseMap(String text) { - if (StringUtils.isBlank(text)) { - return null; - } - try { - return objectMapper.readValue(text, new TypeReference<Map<String, T>>() {}); - } catch (IOException e) { - throw new RuntimeException(e); - } - } + public static Dict parseMap(String text) { + if (StringUtils.isBlank(text)) { + return null; + } + try { + return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); + } catch (MismatchedInputException e) { + // 绫诲瀷涓嶅尮閰嶈鏄庝笉鏄痡son + return null; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static List<Dict> parseArrayMap(String text) { + if (StringUtils.isBlank(text)) { + return null; + } + try { + return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } public static <T> List<T> parseArray(String text, Class<T> clazz) { if (StringUtils.isEmpty(text)) { return new ArrayList<>(); } try { - return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); + return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); } catch (IOException e) { throw new RuntimeException(e); } -- Gitblit v1.9.3