diff --git a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt index 7a5b315c9..f539d7738 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -1,5 +1,34 @@ package io.legado.app.utils +import android.text.TextUtils import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.google.gson.JsonParser -inline fun Gson.fromJson(json: String) = fromJson(json, T::class.java) \ No newline at end of file +inline fun Gson.fromJson(json: String): T = fromJson(json, T::class.java) + +inline fun Gson.arrayFromJson(json: String): ArrayList? = kotlin.run { + var result: ArrayList? = null + if (!TextUtils.isEmpty(json)) { + val gson = GsonBuilder().create() + try { + val parser = JsonParser() + val jArray = parser.parse(json).asJsonArray + jArray?.let { + 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() + } + + } + return result +} \ No newline at end of file