parent
e706053d94
commit
5d162ebfa7
@ -1,34 +1,24 @@ |
|||||||
package io.legado.app.utils |
package io.legado.app.utils |
||||||
|
|
||||||
import android.text.TextUtils |
|
||||||
import com.google.gson.Gson |
import com.google.gson.Gson |
||||||
import com.google.gson.GsonBuilder |
import com.google.gson.GsonBuilder |
||||||
import com.google.gson.JsonParser |
import com.google.gson.JsonParser |
||||||
|
import org.jetbrains.anko.attempt |
||||||
|
|
||||||
|
val GSON: Gson = GsonBuilder().create() |
||||||
|
|
||||||
inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, T::class.java) |
inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, T::class.java) |
||||||
|
|
||||||
inline fun <reified T> Gson.arrayFromJson(json: String): ArrayList<T>? = kotlin.run { |
inline fun <reified T> Gson.arrayFromJson(json: String): ArrayList<T>? = run { |
||||||
var result: ArrayList<T>? = null |
return@run attempt { |
||||||
if (!TextUtils.isEmpty(json)) { |
val result = ArrayList<T>() |
||||||
val gson = GsonBuilder().create() |
val parser = JsonParser() |
||||||
try { |
val jArray = parser.parse(json).asJsonArray |
||||||
val parser = JsonParser() |
jArray?.let { |
||||||
val jArray = parser.parse(json).asJsonArray |
for (obj in it) { |
||||||
jArray?.let { |
attempt { fromJson(obj, T::class.java) }.value?.run { result.add(this) } |
||||||
result = java.util.ArrayList() |
|
||||||
for (obj in it) { |
|
||||||
try { |
|
||||||
val cse = gson.fromJson(obj, T::class.java) |
|
||||||
result?.add(cse) |
|
||||||
} catch (e: Exception) { |
|
||||||
e.printStackTrace() |
|
||||||
} |
|
||||||
} |
|
||||||
} |
} |
||||||
} catch (e: Exception) { |
|
||||||
e.printStackTrace() |
|
||||||
} |
} |
||||||
|
result |
||||||
} |
}.value |
||||||
return result |
|
||||||
} |
} |
Loading…
Reference in new issue