Reputation:
I have to deal with strange Json messages.
there are Arrays in the schema, but if there are only one element array becomes an string.
So sometimes it is:
"Cisco-AVPair": [
"connect-progress=Call Up",
"nas-tx-speed=8083000",
"nas-rx-speed=8083000"
],
and sometimes:
"Cisco-AVPair": "connect-progress=Call Up".
How to overcome this if I use Jackson 1.8.2
I am afraid I am not in control of source code generation and only can parse it.
I do parse it with:
mapper.readValue(json, refType);
while my type reference is:
@JsonProperty("Cisco-AVPair")
private List<String> CiscoAVPair = new ArrayList<String>();
@JsonProperty("Cisco-AVPair")
public List<String> getCiscoAVPair() {
return CiscoAVPair;
}
@JsonProperty("Cisco-AVPair")
public void setCiscoAVPair(List<String> CiscoAVPair) {
this.CiscoAVPair = CiscoAVPair;
}
As you see it is list of strings, but sometimes comes just as a string.
Upvotes: 3
Views: 808
Reputation: 34460
There's a specific config option even in ancient Jackson 1.8.2 that accomplishes exactly what you need.
You should configure your ObjectMapper
instance to always deserialize JSON values as a List
, no matter whether values come as an array or as a single element. Please see javadocs here for the deserialization feature you need to enable, and these other javadocs to see how to actually activate/deactivate a feature on an ObjectMapper
instance.
ObjectMapper mapper = ...;
mapper = mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Bear in mind that configure()
method returns another instance of ObjectMapper
.
Upvotes: 1