I need to deserialize some json which can contain either an array of objects [{},{}] or a single object {}. See my question. Here is what I'm trying to do :
public class LocationDeserializer extends JsonDeserializer<List<Location>>{
@Override
public List<Location> deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException
{
List<Location> list = new ArrayList<Location>();
if(!jp.isExpectedStartArrayToken()){
list.add(...);
}else{
//Populate the list
}
return list;
}
But I'm getting stuck here. How can I remap the object? And how to tell Jackson to use this deserializer for the attribute "location"?
Here is how the Json can look :
{
"location":
[
{
"code":"75",
"type":"1"
},
{
"code":"77",
"type":"1"
}
]
}
or
{
"location":
{
"code":"75",
"type":"1"
}
}