I am trying to find an optimal way to deserialize dynamic fields that all contain the same value object structure:
"data": {
"lastUpdate": 1605299294253,
"tableData": [
{
"recordId": 1,
"isSelected": 1,
"myKey1": {
"valueRef": 72,
"value": "Some value"
},
"myKey2": {
"valueRef": 0,
"value": "123"
}
}
]
}
For the tableData JSON array, I have tried this approach without success:
import lombok.Data;
import java.util.Map;
@Data
public class TableRowDataVO {
private Integer recordId;
private Integer recordStatus;
private Map<String, TableRowDetailVO> columnData1;
private Map<String, TableRowDetailVO> columnData2;
}
This class is nested into another class to encompass the entire JSON body in data:
public class TabularDataDTO {
private Long lastUpdate;
private List<TableRowDataVO> tableData;
}
and each dynamic field holds the same Object of the following type:
public class TableRowDetailVO {
private Integer valueRef;
private String value;
}
There is also no guarantee that there are only 2 dynamic keys. It can be 0 to many dynamic keys that are in TableRowDataVO, but if there are limitations that require me to settle with 2, I can do that, but if 0 to many dynamic fields are possible, that would be great. I am trying to use the ObjectMapper with TypeReference as TabularDataVO but it throws an exception. What is the proper way to design my objects such that TabularDataVO can be deserialized by the mapper?
since the key is dynamic so when you process data you need some special process.
@Data
public class NewDTO {
private Long lastUpdate;
private List<Map<String, Object>> tableData;
}
Just to understand your question correctly. You Json is like following: -
"data": {
"lastUpdate": 1605299294253,
"tableData": [
{
"recordId": 1,
"isSelected": 1,
"myKey1": {
"valueRef": 72,
"value": "Some value"
},
"myKey2": {
"valueRef": 0,
"value": "123"
}
}
]
}
Not like
"data": {
"lastUpdate": 1605299294253,
"tableData": [
{
"recordId": 1,
"isSelected": 1,
"keys": [
{
"valueRef": 72,
"value": "Some value"
},
{
"valueRef": 0,
"value": "123"
}
]
}
]
}
Latter can be easily deserialized using jackson library and defining keys as List<Map<String, Object>>