I am currently working on an app and I would like to have a JSON extraction to a class but it's a little more difficult that just matching field. I am using Moshi for the JSON management.
Below is the JSON I have :
{
"common" :{
"source_url": "xxxx",
"target_url": "yyy
},
"specific":{
"A":{
"setting": "ccc",
},
"B":{
"setting": "ddd",
}
}
}
The goal for me is to get an object based on a data class which look like this:
data class config(
val sourceUrl: String,
val targetUrl: String,
val setting: String
)
What I have started is to first create the different data class to allow me to manipulate the data before getting the above class object.
--GeneralConfig.kt
data class GeneralConfig(
val common: CommonConfig,
val specific: Specific
)
-- CommonConfig.kt
data class CommonConfig(
@Json(val name = "source_url") sourceUrl: String,
@Json(val name = "target_url") val targetUrl: String
)
-- SpecificConfig.kt
data class SpecificConfig(
setting: String,
)
I have 2 main questions:
How can I access the "specific" "A/B" data and get an object class
SpecificConfig. Can I in mygeneralConfigdata class directly extract one field fromspecificand selectAorBby passing an argumentIs it possible to avoid creating multiple data class to get my final class object
config
The purpose is to have a json containing a common data and specific data. I need to build a final data class which a merge from the common data and one of the specific data.
Any idea ? Thanks