I was working with jackson lib in java to deserialize an json file to an array list. First I use this method and everything worked fine.
ObjectMapper objectMapper = new ObjectMapper();
ArrayList<User> users = (ArrayList<User>) objectMapper.readValue(new File("data.json"), new TypeReference<List<User>>() {});
Then I decided to refactor the code and write a generic method to use for any type of data.
public static <T> ArrayList<T> listFromJson(String filename) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return (ArrayList<T>) objectMapper.readValue(new File(filename), new TypeReference<List<T>>() {});
}
This method returns the array list without any exceptions. But when I want to use an element of arraylist and store it in a variable the program throws exception like this.
User user = users.get(0);
Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class org.example.User ...
....
I also tried to print out the element without casting and it wasn't an object reference. It was something like a hashmap.
I think it is related to generics but I don't know the cause. Thanks for your help.