From 7eedf37149b89e6200c5e30abb8dc2575eb6e3d5 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 02 十二月 2021 18:47:18 +0800
Subject: [PATCH] update 优化 pr !118 代码结构
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java | 56 ++++++++++++++++++++++++--------------------------------
1 files changed, 24 insertions(+), 32 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 ae6cc11..b65c23d 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,11 +1,12 @@
package com.ruoyi.common.utils;
-import cn.hutool.core.lang.Validator;
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.ObjectMapper;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import java.io.IOException;
import java.util.ArrayList;
@@ -17,25 +18,15 @@
*
* @author 鑺嬮亾婧愮爜
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonUtils {
- private static ObjectMapper objectMapper = new ObjectMapper();
-
- /**
- * 鍒濆鍖� objectMapper 灞炴��
- * <p>
- * 閫氳繃杩欐牱鐨勬柟寮忥紝浣跨敤 Spring 鍒涘缓鐨� ObjectMapper Bean
- *
- * @param objectMapper ObjectMapper 瀵硅薄
- */
- public static void init(ObjectMapper objectMapper) {
- JsonUtils.objectMapper = objectMapper;
- }
+ private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class);
public static String toJsonString(Object object) {
- if (Validator.isEmpty(object)) {
- return null;
- }
+ if (StringUtils.isNull(object)) {
+ return null;
+ }
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
@@ -44,7 +35,7 @@
}
public static <T> T parseObject(String text, Class<T> clazz) {
- if (StrUtil.isEmpty(text)) {
+ if (StringUtils.isEmpty(text)) {
return null;
}
try {
@@ -66,9 +57,9 @@
}
public static <T> T parseObject(String text, TypeReference<T> typeReference) {
- if (StrUtil.isBlank(text)) {
- return null;
- }
+ if (StringUtils.isBlank(text)) {
+ return null;
+ }
try {
return objectMapper.readValue(text, typeReference);
} catch (IOException e) {
@@ -76,19 +67,20 @@
}
}
- public static <T> Map<String, T> parseMap(String text) {
- if (StrUtil.isBlank(text)) {
- return null;
- }
- try {
- return objectMapper.readValue(text, new TypeReference<Map<String, T>>() {});
- } 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 <T> List<T> parseArray(String text, Class<T> clazz) {
- if (StrUtil.isEmpty(text)) {
+ if (StringUtils.isEmpty(text)) {
return new ArrayList<>();
}
try {
--
Gitblit v1.9.3