From c994e2c97b2ed9bb07490c6b980342af300391df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 15 Feb 2022 12:16:18 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BD=BF=E7=94=A8=20hutool=20Dict=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20JsonUtils=20=E9=98=B2=E6=AD=A2=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=A7=A3=E6=9E=90=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/core/utils/JsonUtils.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java index 2e2368fc..326cd336 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java @@ -1,5 +1,6 @@ package com.ruoyi.common.core.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; @@ -11,7 +12,6 @@ import lombok.NoArgsConstructor; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Map; /** * JSON 工具类 @@ -21,14 +21,14 @@ import java.util.Map; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class JsonUtils { - private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class); + private static ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); public static String toJsonString(Object object) { 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 +39,7 @@ public class JsonUtils { return null; } try { - return objectMapper.readValue(text, clazz); + return OBJECT_MAPPER.readValue(text, clazz); } catch (IOException e) { throw new RuntimeException(e); } @@ -50,7 +50,7 @@ public class JsonUtils { return null; } try { - return objectMapper.readValue(bytes, clazz); + return OBJECT_MAPPER.readValue(bytes, clazz); } catch (IOException e) { throw new RuntimeException(e); } @@ -61,19 +61,29 @@ public class JsonUtils { return null; } try { - return objectMapper.readValue(text, typeReference); + return OBJECT_MAPPER.readValue(text, typeReference); } catch (IOException e) { throw new RuntimeException(e); } } - public static Map parseMap(String text) { + public static Dict parseMap(String text) { if (StringUtils.isBlank(text)) { return null; } try { - return objectMapper.readValue(text, new TypeReference>() { - }); + return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static List 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); } @@ -84,7 +94,7 @@ public class JsonUtils { 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); }