From c12dc71ecea33d5c23e2ceb32bfc3db68e4198e3 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 23 六月 2021 09:44:04 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev'

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 101 insertions(+), 0 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
new file mode 100644
index 0000000..ae6cc11
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java
@@ -0,0 +1,101 @@
+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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JSON 宸ュ叿绫�
+ *
+ * @author 鑺嬮亾婧愮爜
+ */
+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;
+    }
+
+    public static String toJsonString(Object object) {
+		if (Validator.isEmpty(object)) {
+			return null;
+		}
+        try {
+            return objectMapper.writeValueAsString(object);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(String text, Class<T> clazz) {
+        if (StrUtil.isEmpty(text)) {
+            return null;
+        }
+        try {
+            return objectMapper.readValue(text, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
+        if (ArrayUtil.isEmpty(bytes)) {
+            return null;
+        }
+        try {
+            return objectMapper.readValue(bytes, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
+		if (StrUtil.isBlank(text)) {
+			return null;
+		}
+        try {
+            return objectMapper.readValue(text, typeReference);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+	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> List<T> parseArray(String text, Class<T> 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);
+        }
+    }
+
+}

--
Gitblit v1.9.3