Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialise empty array/list?

Tags:

json

jackson

I have a property

@JsonProperty
private Map<String, String> parameters = new HashMap<String, String>();

When I try to deserialise by calling objectMapper.readValue(...) everything works fine until the parameters field in the JSON is empty, ie.

"parameters":[]

I get this exception...

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token

How do I handle empty list? And no I don't have any control over the JSON coming in.

Thanks.

like image 544
TedTrippin Avatar asked Oct 10 '22 04:10

TedTrippin


1 Answers

You can only get a HashMap out of {}, not [] (you should be able to get an ArrayList out of it though).

like image 171
Chris Jester-Young Avatar answered Oct 14 '22 01:10

Chris Jester-Young