I have the following json :
{
"id":"123",
"nbElements":15,
"containers":[
{
"id":"cont1",
"capacity":3
}
],
"operations":[
{
"id":"cont1_01",
"weight":3,
"containerId":"cont1"
},
{
"id":"cont1_02",
"weight": 4,
"containerId":"cont1"
}
]
}
I created the following classes:
data class Instance (
val id: String,
val nbElements: Long,
val containers: List<Container>,
val operations: List<Operation>
)
data class Container (
val id: String,
val capacity: Long
)
data class Operation (
val id: String,
val weight: Long,
val containerId: String
)
And I am parsing it using Moshi
val adapter = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build().adapter<Instance>(Instance::class.java)`
val instance: Instance? = adapter.fromJson(request)
N.B request here is just the json object sent by an API or a Message-oriented middleware
However, I am not happy with that for several reasons.
First, I want to use a HashMap for containers and operations where the keys are the identifiers. Thus I will be able to access an operation object easily using its key.
Second, I want to store the container as a full object in each operation. Thus I can do thins like operation.container.capacity
Any ideas on improving my code?