I have a Json format like:
{
"kind": "prediction",
"command": "true",
"logs": [{
"time_log": "12:00",
"date": "2019-07-17",
"prices": [{
"state": "past",
"count": "128"
}, {
"state": "present",
"type": "255"
}, {
"state": "future",
"count": "300"
}]
}, {
"time_log": "12:00",
"date": "2019-07-18",
"prices": [{
"state": "past",
"count": "255"
}, {
"state": "present",
"type": "308"
}, {
"state": "future",
"count": "400"
}]
}]
}
And classes in Kotlin to represent the parsed data are Response (top-level JSON) and others:
data class Response(
val kind: String,
val command: String,
val logs: List<Log>
)
data class Log(
val time_log: String,
val date: String,
val prices: List<Price>
)
data class Price(
val state: String,
val count: String
)
So the above code works fine when I parse the JSON files normally in Gson without a deserialiser. However, I do not want the Log
class to store a list of Price
s because I know for sure that the prices
array will only contain 3 elements so I want to store each of them in a separate field in the Log
class like:
data class Log(
val time_log: String,
val date: String,
val past_price: Price,
val present_price: Price,
val future_price: Price
)
How can this be done in Gson please? I have been trying for hours but not sure how to go about this. I am also not sure whether I need to write a deserialiser for the Response
class which seems like a lot of work or it can be done more easily? Any pointers or suggestions will be appreciated.