|
|
|
@ -3,20 +3,23 @@ package io.legado.app.utils |
|
|
|
|
import com.google.gson.Gson |
|
|
|
|
import com.google.gson.GsonBuilder |
|
|
|
|
import com.google.gson.JsonParser |
|
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
|
import org.jetbrains.anko.attempt |
|
|
|
|
|
|
|
|
|
val GSON: Gson by lazy { GsonBuilder().create() } |
|
|
|
|
|
|
|
|
|
inline fun <reified T> Gson.fromJsonObject(json: String?): T? = fromJson(json, T::class.java) |
|
|
|
|
inline fun <reified T> genericType() = object : TypeToken<T>() {}.type |
|
|
|
|
|
|
|
|
|
inline fun <reified T> Gson.fromJsonArray(json: String): ArrayList<T>? { |
|
|
|
|
inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型 |
|
|
|
|
return attempt { |
|
|
|
|
with(JsonParser().parse(json).asJsonArray) { |
|
|
|
|
val result = ArrayList<T>() |
|
|
|
|
for (obj in this) { |
|
|
|
|
attempt { fromJson(obj, T::class.java) }.value?.run { result.add(this) } |
|
|
|
|
} |
|
|
|
|
result |
|
|
|
|
} |
|
|
|
|
val result: T? = fromJson(json, genericType<T>()) |
|
|
|
|
result |
|
|
|
|
}.value |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { |
|
|
|
|
return attempt { |
|
|
|
|
val result: List<T>? = fromJson(json, genericType<List<T>>()) |
|
|
|
|
result |
|
|
|
|
}.value |
|
|
|
|
} |