Kotlin Gson Deserializing
Solution 1:
Data Class :
data class Table<T>(
@SerializedName("Table") val models : Array<T>
)
to JSON:
val gson = Gson()
val json = gson.toJson(table)
from JSON:
val json = getJson()
val table = gson.fromJson(json, Table::class.java)
Solution 2:
Method fromJson
is generic, so when you call it for Table<T>
variable it creates Array<Any>
as most suitable. You need to notice that PaymentMethod
class extends T
generic, but I don't know is it even possible. If you find out how to make it, use something like following:
val table: Table<T> = Gson().fromJson<Table<PaymentMethod>>(s)
In your case I'm using gson adapters. Following function creates object with specified type
parameter:
fun getObjectFromString(type: Type, string: String) =
Gson().getAdapter(TypeToken.get(type)).fromJson(string)
To use it write something following:
val table: Table<T> = getObjectFromString(Table<PaymentMethod>::class.java, s) as Table<PaymentMethod>
Update
To avoid spare class cast you can use reified
generic function:
inline fun <reified T> getObjectFromString(string: String): T =
getGsonConverter().getAdapter(TypeToken.get(T::class.java)).fromJson(string)!!
In that case using would be easier:
val table: Table<T> = getObjectFromString<Table<PaymentMethod>>(s)
I used first solution in cases where I don't know what type the object would be - I've got only Type
variable with information about that object.
Solution 3:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to Networking.PaymentMethod[]
Your JSON is
{
"Table": [
{
"paymentmethod_id": 1,
"paymentmethod_description": "Cash",
"paymentmethod_code": "Cash",
"paymentmethod_is_ach_onfile": false,
"paymentmethod_is_element": false,
"paymentmethod_is_reward": false,
"paymentmethod_is_openedgeswipe": false,
"paymentmethod_update_user_id": 1,
"paymentmethod_insert_user_id": 1,
"paymentmethod_insertdate": "2014-10-07 14:53:16",
"paymentmethod_deleted": false,
"paymentmethod_is_mobile_visible": true
}
]
}
Create a data class
, PaymentMethod
.
We frequently create classes whose main purpose is to hold data. In such a class some standard functionality and utility functions are often mechanically derivable from the data.
data class PaymentMethod(@SerializedName("Table") val table:ArrayList<PaymentData> )
data class PaymentData
(
@SerializedName("paymentmethod_id") val paymentmethod_id: Int,
@SerializedName("paymentmethod_description") val paymentmethod_description: String,
@SerializedName("paymentmethod_code") val paymentmethod_code:String,
@SerializedName("paymentmethod_is_ach_onfile") val paidStatus:Boolean,
@SerializedName("paymentmethod_is_element") val paymentmethod_is_element:Boolean,
@SerializedName("paymentmethod_is_reward") val paymentmethod_is_reward:Boolean,
@SerializedName("paymentmethod_is_openedgeswipe") val paymentmethod_is_openedgeswipe:Boolean,
@SerializedName("paymentmethod_update_user_id") val paymentmethod_update_user_id:Int,
@SerializedName("paymentmethod_insert_user_id") val paymentmethod_insert_user_id:Int,
@SerializedName("paymentmethod_insertdate") val paymentmethod_insertdate:String,
@SerializedName("paymentmethod_deleted") val paymentmethod_deleted:Boolean),
@SerializedName("paymentmethod_is_mobile_visible") val paymentmethod_is_mobile_visible:Boolean
)
You can call this way
val paymentDATA = Gson().fromJson<PaymentMethod>("JSON_RESPONSE", PaymentMethod::class.java)
val _adapterPaymentHistory = paymentDATA.table
Post a Comment for "Kotlin Gson Deserializing"