|
|
|
@ -4,6 +4,8 @@ import com.google.gson.Gson |
|
|
|
|
import com.google.gson.GsonBuilder |
|
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
|
import org.jetbrains.anko.attempt |
|
|
|
|
import java.lang.reflect.ParameterizedType |
|
|
|
|
import java.lang.reflect.Type |
|
|
|
|
|
|
|
|
|
val GSON: Gson by lazy { |
|
|
|
|
GsonBuilder() |
|
|
|
@ -23,7 +25,15 @@ inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意 |
|
|
|
|
|
|
|
|
|
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { |
|
|
|
|
return attempt { |
|
|
|
|
val result: List<T>? = fromJson(json, genericType<List<T>>()) |
|
|
|
|
val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java)) |
|
|
|
|
result |
|
|
|
|
}.value |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class ParameterizedTypeImpl(val clz: Class<*>) : ParameterizedType { |
|
|
|
|
override fun getRawType(): Type = List::class.java |
|
|
|
|
|
|
|
|
|
override fun getOwnerType(): Type? = null |
|
|
|
|
|
|
|
|
|
override fun getActualTypeArguments(): Array<Type> = arrayOf(clz) |
|
|
|
|
} |
|
|
|
|