package io.renren.common.utils;


import com.google.gson.*;
import com.google.gson.internal.bind.ObjectTypeAdapter;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Gson 处理
 *
 * @author chenrj
 * @email 1412736935@qq.com
 * @since 2021/7/22
 */
public class GsonUtil {

    private static Gson gson;
    private static Gson prettyGson;

    static {
        gson = getGson();
        prettyGson = new GsonBuilder().setPrettyPrinting().create();
    }

    /**
     * 私有方法
     */
    private GsonUtil() {

    }

    /**
     * 使用自定义工厂方法取代 Gson 实例中的工厂方法
     *
     * @return
     */
    public static Gson getGson() {
        Gson gson = new GsonBuilder().create();
        try {
            Field factories = Gson.class.getDeclaredField("factories");
            factories.setAccessible(true);
            Object o = factories.get(gson);
            Class<?>[] declaredClasses = Collections.class.getDeclaredClasses();
            for (Class c : declaredClasses) {
                if ("java.util.Collections$UnmodifiableList".equals(c.getName())) {
                    Field listField = c.getDeclaredField("list");
                    listField.setAccessible(true);
                    List<TypeAdapterFactory> list = (List<TypeAdapterFactory>) listField.get(o);
                    int i = list.indexOf(ObjectTypeAdapter.FACTORY);
                    list.set(i, MapTypeAdapter.FACTORY);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gson;
    }

    /**
     * 将object对象转成json字符串
     *
     * @param object model 类
     */
    public static String toString(Object object) {
        String result = null;
        if (gson != null) {
            result = gson.toJson(object);
        }
        return result;
    }

    /**
     * 将object对象转成json字符串，带有格式化
     *
     * @param object model 类
     */
    public static String toPrettyString(Object object) {
        String result = null;
        if (prettyGson != null) {
            result = prettyGson.toJson(object);
        }
        return result;
    }


    /**
     * 将gsonString转成泛型bean
     *
     * @param content string 内容
     * @param cls     类
     */
    public static <T> T convertJson(String content, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(content, cls);
        }
        return t;
    }

    /**
     * 转成map的
     *
     * @param content string 内容
     * @return Map
     */
    public static <T> Map<String, T> convertJsonToMap(String content) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(content, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }

    /**
     * 转换为JsonObject
     *
     * @param content 文本内容
     */
    public static JsonObject convertJsonObject(String content) {
        return JsonParser.parseString(content).getAsJsonObject();
    }

    /**
     * 转换为JsonArray
     *
     * @param content 文本内容
     */
    public static JsonArray convertJsonArray(String content) {
        return JsonParser.parseString(content).getAsJsonArray();
    }
}