How to filter to show only required fields in a class using Gson

Asked

Viewed 53 times

1

I’m using Gson to work with json, let’s say I have this class

class Track(
    @SerializedName("id") val id: Long,
    @SerializedName("name") val name: String,
    @SerializedName("modality") val modality: String,
    @SerializedName("paper") val paper: Int,
    @SerializedName("popper") val popper: Int,
    @SerializedName("plate") val plate: Int,
    @SerializedName("disappear") val disappear: Int,
    @SerializedName("minRound") val minRound: Int,
    @SerializedName("penalty") val penalty: Int,
    @SerializedName("maxPoint") val maxPoint: Int,
    @SerializedName("order") val order: Int)

and I’m using it to catch all the tracks

 val trackType = object : TypeToken<List<Track>>() {}.type
 val modalities = Gson().fromJson<List<Track>>("json", trackType)

the more it brings me all the fields, as I do to type filter only type to get the id and the name?

1 answer

1

An alternative if you don’t want to deal with the object with all fields, vc can create a hashMap and insert the list data into it, using a key, value and then destroy the "complete list"

    private val mPriorityCache = hashMapOf<Int, String>()
    fun setCache(list: List<PriorityEntity>) {
        for (item in list) {
            mPriorityCache.put(item.id, item.description)
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.