Ver código fonte

代码优化

wangliang 2 anos atrás
pai
commit
8f58ac7fc3

+ 30 - 0
themis-common/src/main/java/com/qmth/themis/common/util/GsonUtil.java

@@ -0,0 +1,30 @@
+package com.qmth.themis.common.util;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+
+import java.lang.reflect.Type;
+
+/**
+ * @Description: Json工具类
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/10/12
+ */
+public class GsonUtil {
+    private static Gson gson = new GsonBuilder().create();
+
+    public static String toJson(Object value) {
+        return gson.toJson(value);
+    }
+
+    public static <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
+        return gson.fromJson(json, classOfT);
+    }
+
+    public static <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
+        return (T) gson.fromJson(json, typeOfT);
+    }
+}