Android-Gson利用

藏宝库编辑 2024-10-7 22:51:05 102 0 来自 中国
在一样平常的Android开发当中,我们肯定少不了要利用Gson框架分析JSON字符串。这篇博客分享一下我相识到Gson框架的知识,渴望对看文章的小搭档有所资助。
Gson的依赖
implementation 'com.google.code.gson:gson:2.9.0'
这里必要注意的是:
Gson2.9.0最低适配Java7,假如想要是用Java6及其以下的版本要利用Gson2.8.9。
利用Gson开发的时间,我保举一个工具类:
package com.blankj.utilcode.util;import android.text.TextUtils;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.reflect.TypeToken;import java.io.Reader;import java.lang.reflect.Type;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import androidx.annotation.NonNull;/** * <pre> *     author: Blankj *     blog  : http://blankj.com *     time  : 2018/04/05 *     desc  : utils about gson * </pre> */public final class GsonUtils {    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<>();    private GsonUtils() {        throw new UnsupportedOperationException("u can't instantiate me...");    }    /**     * Set the delegate of {@link Gson}.     *     * @param delegate The delegate of {@link Gson}.     */    public static void setGsonDelegate(Gson delegate) {        if (delegate == null) return;        GSONS.put(KEY_DELEGATE, delegate);    }    /**     * Set the {@link Gson} with key.     *     * @param key  The key.     * @param gson The {@link Gson}.     */    public static void setGson(final String key, final Gson gson) {        if (TextUtils.isEmpty(key) || gson == null) return;        GSONS.put(key, gson);    }    /**     * Return the {@link Gson} with key.     *     * @param key The key.     * @return the {@link Gson} with key     */    public static Gson getGson(final String key) {        return GSONS.get(key);    }    public static Gson getGson() {        Gson gsonDelegate = GSONS.get(KEY_DELEGATE);        if (gsonDelegate != null) {            return gsonDelegate;        }        Gson gsonDefault = GSONS.get(KEY_DEFAULT);        if (gsonDefault == null) {            gsonDefault = createGson();            GSONS.put(KEY_DEFAULT, gsonDefault);        }        return gsonDefault;    }    /**     * Serializes an object into json.     *     * @param object The object to serialize.     * @return object serialized into json.     */    public static String toJson(final Object object) {        return toJson(getGson(), object);    }    /**     * Serializes an object into json.     *     * @param src       The object to serialize.     * @param typeOfSrc The specific genericized type of src.     * @return object serialized into json.     */    public static String toJson(final Object src, @NonNull final Type typeOfSrc) {        return toJson(getGson(), src, typeOfSrc);    }    /**     * Serializes an object into json.     *     * @param gson   The gson.     * @param object The object to serialize.     * @return object serialized into json.     */    public static String toJson(@NonNull final Gson gson, final Object object) {        return gson.toJson(object);    }    /**     * Serializes an object into json.     *     * @param gson      The gson.     * @param src       The object to serialize.     * @param typeOfSrc The specific genericized type of src.     * @return object serialized into json.     */    public static String toJson(@NonNull final Gson gson, final Object src, @NonNull final Type typeOfSrc) {        return gson.toJson(src, typeOfSrc);    }    /**     * Converts {@link String} to given type.     *     * @param json The json to convert.     * @param type Type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(final String json, @NonNull final Class<T> type) {        return fromJson(getGson(), json, type);    }    /**     * Converts {@link String} to given type.     *     * @param json the json to convert.     * @param type type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(final String json, @NonNull final Type type) {        return fromJson(getGson(), json, type);    }    /**     * Converts {@link Reader} to given type.     *     * @param reader the reader to convert.     * @param type   type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Reader reader, @NonNull final Class<T> type) {        return fromJson(getGson(), reader, type);    }    /**     * Converts {@link Reader} to given type.     *     * @param reader the reader to convert.     * @param type   type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Reader reader, @NonNull final Type type) {        return fromJson(getGson(), reader, type);    }    /**     * Converts {@link String} to given type.     *     * @param gson The gson.     * @param json The json to convert.     * @param type Type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Gson gson, final String json, @NonNull final Class<T> type) {        return gson.fromJson(json, type);    }    /**     * Converts {@link String} to given type.     *     * @param gson The gson.     * @param json the json to convert.     * @param type type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Gson gson, final String json, @NonNull final Type type) {        return gson.fromJson(json, type);    }    /**     * Converts {@link Reader} to given type.     *     * @param gson   The gson.     * @param reader the reader to convert.     * @param type   type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Gson gson, final Reader reader, @NonNull final Class<T> type) {        return gson.fromJson(reader, type);    }    /**     * Converts {@link Reader} to given type.     *     * @param gson   The gson.     * @param reader the reader to convert.     * @param type   type type json will be converted to.     * @return instance of type     */    public static <T> T fromJson(@NonNull final Gson gson, final Reader reader, @NonNull final Type type) {        return gson.fromJson(reader, type);    }    /**     * Return the type of {@link List} with the {@code type}.     *     * @param type The type.     * @return the type of {@link List} with the {@code type}     */    public static Type getListType(@NonNull final Type type) {        return TypeToken.getParameterized(List.class, type).getType();    }    /**     * Return the type of {@link Set} with the {@code type}.     *     * @param type The type.     * @return the type of {@link Set} with the {@code type}     */    public static Type getSetType(@NonNull final Type type) {        return TypeToken.getParameterized(Set.class, type).getType();    }    /**     * Return the type of map with the {@code keyType} and {@code valueType}.     *     * @param keyType   The type of key.     * @param valueType The type of value.     * @return the type of map with the {@code keyType} and {@code valueType}     */    public static Type getMapType(@NonNull final Type keyType, @NonNull final Type valueType) {        return TypeToken.getParameterized(Map.class, keyType, valueType).getType();    }    /**     * Return the type of array with the {@code type}.     *     * @param type The type.     * @return the type of map with the {@code type}     */    public static Type getArrayType(@NonNull final Type type) {        return TypeToken.getArray(type).getType();    }    /**     * Return the type of {@code rawType} with the {@code typeArguments}.     *     * @param rawType       The raw type.     * @param typeArguments The type of arguments.     * @return the type of map with the {@code type}     */    public static Type getType(@NonNull final Type rawType, @NonNull final Type... typeArguments) {        return TypeToken.getParameterized(rawType, typeArguments).getType();    }    static Gson getGson4LogUtils() {        Gson gson4LogUtils = GSONS.get(KEY_LOG_UTILS);        if (gson4LogUtils == null) {            gson4LogUtils = new GsonBuilder().setPrettyPrinting().serializeNulls().create();            GSONS.put(KEY_LOG_UTILS, gson4LogUtils);        }        return gson4LogUtils;    }    private static Gson createGson() {        return new GsonBuilder().serializeNulls().disableHtmlEscaping().create();    }}这个Gson工具类泉源于AndroidUtilCode工具类开源库,主要实现了一下几种功能呢:

  • 设置署理对象:setGsonDelegate();
  • 设置Gson对象:setGson();
  • 获取Gson对象:getGson();
  • 对账转Json字符串:toJson(),这个方法常用于服务器哀求利用Json转达参数;
  • Json转对象:fromJson();
  • 获取链表范例:getListType();
  • 获取聚集范例:getSetType();
  • 获取字典范例:getMapType();
  • 获取数组范例:getArrayType();
  • 获取范例:getType()。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-11-22 00:29, Processed in 0.167138 second(s), 33 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表