How to change the field name of `Pair`? The code ...
# android
a
How to change the field name of
Pair
? The code like this :
Copy code
val distributorNotes = ArrayList<PairDistributorNotes<String, String>>()

distributorNotes.add(PairDistributorNotes("350631771", "test"))

data class PairDistributorNotes<out x, out y>(val distributor_id: x, val note: y) {
    override fun toString(): String = "($distributor_id, $note)"
}
and i create
asReverse(
) with
distinctBy
Copy code
val dataJson = Gson().toJson(distributorNotes.asReversed().distinctBy { it.distributor_id })
Output from dataJson :
[{"a":"350631771","b":"test"}]
Can field a and b change to distributor_id and note ? Expected :
[{"distributor_id":"350631771","note":"test"}]
UPDATE Based on Christian Grach answer, because Proguard is enabled, Gson require annotations to preserve the field name.. We need to add
@field:SerializedName()
on
data class
Copy code
data class PairDistributorNotes<out A, out B>(
      @field:SerializedName("distributor_id")
      val distributor_id: A,

      @field:SerializedName("note")
      val note: B
) {
      override fun toString(): String = "($distributor_id, $note)"
}
c
Do you have Proguard or R8 enabled? I guess Gson has some annotations to preserve the field name.
1
a
Yes i have Proguard enabled, how to prevent that for Gson?
c
Like I said, there should be annotations that you apply on the properties.
google
a
Ah, I see.. My bad.. it's working with it
@field:SerializedName()
thanks!