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

1

I'm using Gson to work with json, let's say it has 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)

I'm using this to get all Tracks

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

But it brings me all the fields, how do I sort just type sort to get id and name?

    
asked by anonymous 13.08.2018 / 23:07

1 answer

1

An alternative if you do not want to deal with the object with all the fields, you can create a hashMap and insert the list data in 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)
        }
    }
    
17.08.2018 / 14:53