From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001
From: lee <4766465@qq.com>
Date: Wed, 18 Dec 2024 13:27:00 +0800
Subject: [PATCH] init

---
 xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/GsonTool.java |   88 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/GsonTool.java b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/GsonTool.java
new file mode 100644
index 0000000..85682a6
--- /dev/null
+++ b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/GsonTool.java
@@ -0,0 +1,88 @@
+package com.xxl.job.core.util;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.List;
+
+/**
+ * @author xuxueli 2020-04-11 20:56:31
+ */
+public class GsonTool {
+
+    private static Gson gson = null;
+    static {
+            gson= new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
+    }
+
+    /**
+     * Object 转成 json
+     *
+     * @param src
+     * @return String
+     */
+    public static String toJson(Object src) {
+        return gson.toJson(src);
+    }
+
+    /**
+     * json 转成 特定的cls的Object
+     *
+     * @param json
+     * @param classOfT
+     * @return
+     */
+    public static <T> T fromJson(String json, Class<T> classOfT) {
+        return gson.fromJson(json, classOfT);
+    }
+
+    /**
+     * json 转成 特定的 rawClass<classOfT> 的Object
+     *
+     * @param json
+     * @param classOfT
+     * @param argClassOfT
+     * @return
+     */
+    public static <T> T fromJson(String json, Class<T> classOfT, Class argClassOfT) {
+        Type type = new ParameterizedType4ReturnT(classOfT, new Class[]{argClassOfT});
+        return gson.fromJson(json, type);
+    }
+    public static class ParameterizedType4ReturnT implements ParameterizedType {
+        private final Class raw;
+        private final Type[] args;
+        public ParameterizedType4ReturnT(Class raw, Type[] args) {
+            this.raw = raw;
+            this.args = args != null ? args : new Type[0];
+        }
+        @Override
+        public Type[] getActualTypeArguments() {
+            return args;
+        }
+        @Override
+        public Type getRawType() {
+            return raw;
+        }
+        @Override
+        public Type getOwnerType() {return null;}
+    }
+
+    /**
+     * json 转成 特定的cls的list
+     *
+     * @param json
+     * @param classOfT
+     * @return
+     */
+    public static <T> List<T> fromJsonList(String json, Class<T> classOfT) {
+        return gson.fromJson(
+                json,
+                new TypeToken<List<T>>() {
+                }.getType()
+        );
+    }
+
+}

--
Gitblit v1.9.3