I have some JSON that looks like this:
{
"name" : "Credit Card",
"code" : "AUD",
"value" : 1000
}
and am using Moshi to unmarshall this into a data structure like:
data class Account(
@Json(name = "name")
val name: String,
@Json(name = "currency")
val currency: String,
@Json(name = "value")
val value: Int
)
Everything works well. However, I really would like to extract the currency
and value
parameters into a separate Money
object. So my model looks more like:
data class Money(
@Json(name = "currency")
val currency: String,
@Json(name = "value")
val value: Int
)
data class Account(
@Json(name = "name")
val name: String,
@Json(name = "???")
val money: Money
)
The challenge I'm struggling with is how to annotate things so that the Money
object can be given two different fields (currency
and value
) that come from the same level as the parent account.
Do I need to create an intermediate "unmarshalling" object called, say, MoshiAccount
and then use a custom adapter to convert that to my real Account
object?
I saw How to deseralize an int array into a custom class with Moshi? which looks close (except that in that case, the adapted object (VideoSize) only needs a single field as input... in my case, I need both currency
and value
)
Any thoughts or suggestions would be much appreciated. Thanks