Jackson - Deserialising JSON string - TypeReference vs TypeFactory.constructCollectionType

Viewed 30059

To deserialise JSON string to a list of class, different ways listed at StackOverflow question

Type 1 (docs link):

List<SomeClass> someClassList = mapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));

Type 2 (docs link):

List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });

Though both 2 types above do the job, what is the difference between these implementations ?

2 Answers

One more way you can achieve this is by :

List<SomeClass> list = Arrays.asList(mapper.readValue(jsonString, SomeClass[].class));

Related