I want to use Jackson to parse a list of objects. With Gson, I can do this easily:
List<MyType> list = new Gson().fromJson(input, Types.listOf(MyType.class));
With Jackson, it seems to be more difficult:
List<MyType> list = new ObjectMapper().readValue(input, /* What to put here? */);
The code needs to work for lists of any type (like Gson does), so I can't just make a type that contains a list of MyType and pass that in.
I've tried using new TypeLiteral<List<MyType>>(){}, which works, but only for a single type. If I pass in the type to the method, it doesn't work any more:
public <T> List<T> parse(Class<T> myType) {
// returns a List<Map<?,?>> instead of List<T>
return new ObjectMapper().readValue(input, new TypeLiteral<List<T>>(){});
}
How do I do this with Jackson?