diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java index c3eb0684c..07ccaa0e5 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java @@ -2,8 +2,11 @@ import android.text.TextUtils; +import androidx.annotation.NonNull; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import java.io.Reader; @@ -13,8 +16,6 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import androidx.annotation.NonNull; - /** *
@@ -26,8 +27,8 @@
*/
public final class GsonUtils {
- private static final String KEY_DEFAULT = "defaultGson";
- private static final String KEY_DELEGATE = "delegateGson";
+ private static final String KEY_DEFAULT = "defaultGson";
+ private static final String KEY_DELEGATE = "delegateGson";
private static final String KEY_LOG_UTILS = "logUtilsGson";
private static final Map GSONS = new ConcurrentHashMap<>();
@@ -168,6 +169,28 @@ public static T fromJson(@NonNull final Reader reader, @NonNull final Type t
return fromJson(getGson(), reader, type);
}
+ /**
+ * Converts {@link JsonElement} to given type.
+ *
+ * @param json the JsonElement to convert.
+ * @param classOfT The class of T
+ * @return instance of type
+ */
+ public static T fromJson(JsonElement json, Class classOfT) {
+ return fromJson(getGson(), json, classOfT);
+ }
+
+ /**
+ * Converts {@link JsonElement} to given type.
+ *
+ * @param json the JsonElement to convert.
+ * @param typeOfT The specific genericized type of src.
+ * @return instance of type
+ */
+ public static T fromJson(JsonElement json, Type typeOfT) {
+ return fromJson(getGson(), json, typeOfT);
+ }
+
/**
* Converts {@link String} to given type.
*
@@ -216,6 +239,30 @@ public static T fromJson(@NonNull final Gson gson, final Reader reader, @Non
return gson.fromJson(reader, type);
}
+ /**
+ * Converts {@link JsonElement} to given type.
+ *
+ * @param gson The gson.
+ * @param json the JsonElement to convert.
+ * @param classOfT The class of T
+ * @return instance of type
+ */
+ public static T fromJson(@NonNull final Gson gson, JsonElement json, Class classOfT) {
+ return gson.fromJson(json, classOfT);
+ }
+
+ /**
+ * Converts {@link JsonElement} to given type.
+ *
+ * @param gson The gson.
+ * @param json the JsonElement to convert.
+ * @param typeOfT The specific genericized type of src.
+ * @return instance of type
+ */
+ public static T fromJson(@NonNull final Gson gson, JsonElement json, Type typeOfT) {
+ return gson.fromJson(json, typeOfT);
+ }
+
/**
* Return the type of {@link List} with the {@code type}.
*