'Jackson: deserialization of object arrays
I need to deserialize a given JSON structure like following into POJOs:
[
{
"alpha": [
{
"serial": 123,
"date": "24-01-2022",
"foo": "...",
...
}
]
},
{
"beta": [
{
"serial": 456,
"date": "24-01-2022",
"bar: "...",
...
}
]
},
{
"gamma": [
{
"serial": 789,
"date": "24-01-2022",
"foobar": "...""
...
}
]
},
{
"key1": "...",
"key2": "..."
}
]
Alpha, beta, gamma are arrays of different objects that could share a common base type as they have some common properties. However, the last object in the JSON array is different.
The type of the objects in the arrays are determined by the field name (e.g. alpha, betta, gamma). Jackson provides a way to define the type of the object using a "wrapper object" (@JsonTypeInfo(use = Id.CUSTOM, include = As.WRAPPER_OBJECT)), but this assumes the value is an object and not an array.
Currently, I use a list of the following object to deserialize the JSON and interate over it.
public class Status {
@JsonProperty
List<AlphaStatus> alpha;
@JsonProperty
List<BettaStatus> betta;
@JsonProperty
List<GammaStatus> gamma;
@JsonProperty
String key1;
@JsonProperty
String key2;
}
Is there a more elegant solution to this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
