|
|
@ -2,6 +2,7 @@ package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson |
|
|
|
import com.google.gson.Gson |
|
|
|
import com.google.gson.GsonBuilder |
|
|
|
import com.google.gson.GsonBuilder |
|
|
|
|
|
|
|
import com.google.gson.JsonSyntaxException |
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
import org.jetbrains.anko.attempt |
|
|
|
import org.jetbrains.anko.attempt |
|
|
|
import java.lang.reflect.ParameterizedType |
|
|
|
import java.lang.reflect.ParameterizedType |
|
|
@ -16,6 +17,8 @@ val GSON: Gson by lazy { |
|
|
|
|
|
|
|
|
|
|
|
inline fun <reified T> genericType() = object : TypeToken<T>() {}.type |
|
|
|
inline fun <reified T> genericType() = object : TypeToken<T>() {}.type |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Throws(JsonSyntaxException::class) |
|
|
|
inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型 |
|
|
|
inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型 |
|
|
|
return attempt { |
|
|
|
return attempt { |
|
|
|
val result: T? = fromJson(json, genericType<T>()) |
|
|
|
val result: T? = fromJson(json, genericType<T>()) |
|
|
@ -23,6 +26,7 @@ inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意 |
|
|
|
}.value |
|
|
|
}.value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Throws(JsonSyntaxException::class) |
|
|
|
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { |
|
|
|
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { |
|
|
|
return attempt { |
|
|
|
return attempt { |
|
|
|
val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java)) |
|
|
|
val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java)) |
|
|
|