I have the following JSON:
{
"item": [
{ "foo": 1 },
{ "foo": 2 }
]
}
This is basically an object that contains a collection of items.
So I made a class to deserialize that:
public class ItemList {
@JsonProperty("item")
List<Item> items;
// Getters, setters & co.
// ...
}
Everything is working nicely up to this point.
Now, To make my life easier somewhere else, I decided that it would be nice to be able to iterate on the ItemList object and let it implement the Collection interface.
So basically my class became:
public class ItemList implements Collection<Item>, Iterable<Item> {
@JsonProperty("item")
List<Item> items;
// Getters, setters & co.
// Generated all method delegates to items. For instance:
public Item get(int position) {
return items.get(position);
}
}
The implementation works properly and nicely. However, the deserialization now fails.
Looks like Jackson is getting confused:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.ItemList out of START_OBJECT token
I have tried to add @JsonDeserialize(as=ItemList.class)
but it did not do the trick.
What's the way to go?