0

I'm still struggling with some of the basic uses of Generics, this time with Jackson. Neither of these work right. The first returns a list of LinkedHashMaps instead of a list of type T. The second can't see the variable "type" on the last line.

Can anyone tell me what I'm doing wrong :

public <T> List<T> readObjects(File file)
{
    File file = new File(subscriptionDir, fileName)
    return mapper.readValue(file, new TypeReference<List<T>>() {})
}


public <T> List<T> readObjects(String fileName, Class<T> type)
{
    File file = new File(subscriptionDir, fileName)
    return mapper.readValue(file, new TypeReference<List<type>>(){}) 
}

Thanks in advanced for any assistance.

solvingJ
  • 1,321
  • 1
  • 19
  • 30
  • The duplicate discusses Gson's `TypeToken` which is equivalent to Jackson's `TypeReference`. You cant do this using `TypeReference` nor the generic type parameter. You need concrete types. You can use the `ObjectMapper` to construct a `Type` that represents a `List` if you have the `Class` object for `T`. – Sotirios Delimanolis Sep 17 '16 at 15:30
  • The solution proposed by varren looks like what I'm trying to do. However, I'm actually using groovy, and it's detecting the curly braces as a closure: {childrenClazz} . Any ideas? – solvingJ Sep 17 '16 at 15:35
  • That's just an array initializer. Use whatever the groovy syntax for creating arrays is. – Sotirios Delimanolis Sep 17 '16 at 15:37
  • mapper.readValue(file,type) says it doesn't have a signature that takes a file , ParameterizedTypeImpl – solvingJ Sep 17 '16 at 16:29
  • Used Answer from staxman , was the best. Construct a JavaType first, then pass that to the readValue method. http://stackoverflow.com/questions/6846244/jackson-and-generic-type-reference – solvingJ Sep 17 '16 at 16:38

0 Answers0