Aldan Rizki Santosa
05/29/2021, 3:29 PMPair
?
The code like this :
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
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
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)"
}
Chrimaeon
05/29/2021, 4:05 PMAldan Rizki Santosa
05/29/2021, 4:09 PMChrimaeon
05/29/2021, 4:10 PMChrimaeon
05/29/2021, 4:11 PMAldan Rizki Santosa
05/29/2021, 4:32 PM@field:SerializedName()
thanks!