Skip to content Skip to sidebar Skip to footer

Kotlin Gson Deserializing

I am receiving a JSON data model that has a map wrapper Table. I'm trying to use generics to pass in the type that is beyond the wrapper but it's not translating well at runtime. H

Solution 1:

Data Class :

dataclassTable<T>(
    @SerializedName("Table")val models : Array<T>
)

to JSON:

valgson= Gson()
valjson= 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:

fungetObjectFromString(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) asTable<PaymentMethod>

Update

To avoid spare class cast you can use reified generic function:

inlinefun<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.

dataclassPaymentMethod(@SerializedName("Table") val table:ArrayList<PaymentData> )
dataclassPaymentData

                      (
                       @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") valpaymentmethod_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"