Created
November 26, 2019 11:05
-
-
Save fida1989/e4ae96950c0e8b6946015d22b8c36e47 to your computer and use it in GitHub Desktop.
gson singelton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import androidx.annotation.NonNull; | |
| import com.google.gson.Gson; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import java.lang.reflect.ParameterizedType; | |
| import java.lang.reflect.Type; | |
| import java.util.ArrayList; | |
| public class GlobalGson { | |
| private Gson gson = new Gson(); | |
| private static final GlobalGson globalGson = new GlobalGson(); | |
| public static GlobalGson get() { | |
| return globalGson; | |
| } | |
| private GlobalGson() { | |
| } | |
| private Gson getGson() { | |
| if (gson == null) { | |
| gson = new Gson(); | |
| } | |
| return gson; | |
| } | |
| public <T> String toJson(@NonNull final T t) { | |
| return getGson().toJson(t); | |
| } | |
| public <T> JsonObject getAsJsonObject(@NonNull final T t) { | |
| return getGson().toJsonTree(t).getAsJsonObject(); | |
| } | |
| public <T> T fromJson(@NonNull final String o, @NonNull Class<T> tClass) { | |
| return getGson().fromJson(o, tClass); | |
| } | |
| public <T> T fromJson(@NonNull final JsonElement o, @NonNull Class<T> tClass) { | |
| return getGson().fromJson(o, tClass); | |
| } | |
| public <E> ArrayList<E> listFromJson(@NonNull final String o, @NonNull final Type listType) { | |
| return getGson().fromJson(o, listType); | |
| } | |
| public <E> ArrayList<E> listFromJson(@NonNull final JsonElement o, @NonNull final Type listType) { | |
| return getGson().fromJson(o, listType); | |
| } | |
| public <E> ArrayList<E> listFromJson(@NonNull final String o, @NonNull final Class<E> eClass) { | |
| return getGson().fromJson(o, new ListType<>(eClass)); | |
| } | |
| public <E> ArrayList<E> listFromJson(@NonNull final JsonElement o, @NonNull final Class<E> eClass) { | |
| return getGson().fromJson(o, new ListType<>(eClass)); | |
| } | |
| private static class ListType<E> implements ParameterizedType { | |
| private Class<?> wrapped; | |
| private ListType(Class<E> wrapped) { | |
| this.wrapped = wrapped; | |
| } | |
| public Type[] getActualTypeArguments() { | |
| return new Type[]{wrapped}; | |
| } | |
| public Type getRawType() { | |
| return ArrayList.class; | |
| } | |
| public Type getOwnerType() { | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment