Daniele Andreoli
06/13/2022, 8:54 AM@Serializable(with = PaymentInfoSerializer::class)
data class PaymentInfo(
val id: PaymentId,
val amount: Float,
val paidAt: String?,
val productId: ProductId
)
The last field is a typealias for a Int type but it’s actually an evaluated one. The field is a field taken from an inner Json Object like this:
"product":{"id":74}
so the value is just the id field.
The entire json is like this one:
"id":22,"paidAt":null,"amount":720.0,"description":null,"product":{"id":74}
So my problem is that I can’t use simple deserilization with dataclass because I have to keep the code working but i can’t figure out how a map object is actually descripted inside a desirializer.
I’have written a descriptor like this but i’m not sure how ti could work as I have no way to tell it to take just the id field inside the map:
override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("PaymentInfo") {
element<PaymentId>("id")
element<Float>("amount")
element<String?>("paidAt", isOptional = true)
element<Map<String, Int>>("product")
}
At the end, i’ve to find a way to tell serilization to do something like this in GSON:
productId = jsonObject.get("product").asJsonObject.get("id").asInt
I've read the doc but I can't find some example that actually seems to fit and since I have to migrate a lot of this custom serializer I'm tring to start with the correct structure.
Is there someone who could help? Thanksribesg
06/13/2022, 2:00 PMJsonObject
as the typeDaniele Andreoli
06/13/2022, 2:08 PMdescriptor
definition?ribesg
06/13/2022, 2:32 PMribesg
06/13/2022, 2:33 PMDaniele Andreoli
06/13/2022, 3:08 PM