I interact with a set of APIs that all return a similar response:
{
"common1": ...,
"common2": ...,
"common3": ...,
"someList": [
//polymorph objects here
]
}
Currently, I have a separate POJO for each of these responses. However I would like to factorize this in a common object since I always perform the same actions with such object and that makes a lot of code duplication.
Initially, I had thought to create a similar Jackson class:
public class CommonClass<T> {
@JsonProperty("common1")
private final CommonType1 common1;
@JsonProperty("common2")
private final CommonType2 common2;
@JsonProperty("common3")
private final CommonType3 common3;
@JsonProperty("someList")
private final List<T> someList;
//constructor and getters here
}
This would work fine, except for an issue: the Json name of someList
changes as well. Sometimes it’s someListA
, sometimes it’s someListB
… basically it changes depending on the type it contains.
That makes it impossible to follow this approach (at least I think) since I can’t dynamically change the value of the JsonProperty()
annotations in my POJO.
I know that I could create a base class and then multiple extensions of such class, each one with a different list (differently named), but that wouldn’t be my ideal solution because I wouldn’t be able to have a common getter to process the elements without downcasting to the concrete type of POJO that I’m handling.
Do you have any idea/suggestion/approach for such problem?
2
Answers
The simplest way is to deserialize it as a
Map<String, Object>
and than treat your map the same way except when retrieving someList* property instead of looking for a particular key such assomeListA
orSomeListB
just search for the key that starts with"someList"
prefixIf the list of options for the name is not too long, you can use JsonAlias.
Example: