Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,8 +16,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import androidx.annotation.NonNull;


/**
* <pre>
Expand All @@ -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<String, Gson> GSONS = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -168,6 +169,28 @@ public static <T> 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> T fromJson(JsonElement json, Class<T> 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> T fromJson(JsonElement json, Type typeOfT) {
return fromJson(getGson(), json, typeOfT);
}

/**
* Converts {@link String} to given type.
*
Expand Down Expand Up @@ -216,6 +239,30 @@ public static <T> 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> T fromJson(@NonNull final Gson gson, JsonElement json, Class<T> 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> 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}.
*
Expand Down