package com.gh.gamecenter.core.utils

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import org.json.JSONArray


/**
 * Created by khy on 11/10/17.
 */
object GsonUtils {

    @JvmStatic
    val gson by lazy { Gson() }
    val gsonThatIgnoreNull by lazy { GsonBuilder().serializeNulls().create() }

    @JvmStatic
    fun <T> fromJson(json: String, t: Class<T>): T {
        return gson.fromJson(json, t)
    }

    @JvmStatic
    fun <T> fromJsonList(json: JSONArray): List<T> {
        return try {
            val type = object : TypeToken<List<T>>() {}.type
            return gson.fromJson(json.toString(), type)
        } catch (e: Exception) {
            listOf()
        }
    }

    @JvmStatic
    fun <T> fromJsonList(json: String): List<T> {
        return try {
            val type = object : TypeToken<List<T>>() {}.type
            gson.fromJson(json, type)
        } catch (e: Exception) {
            listOf()
        }
    }

    @JvmStatic
    fun toJson(any: Any?): String {
        return gson.toJson(any)
    }

    @JvmStatic
    fun toJsonIgnoreNull(any: Any?): String {
        return gsonThatIgnoreNull.toJson(any)
    }
}