I know there is a lot of content on this throughout the web but I couldn't find one that will help me solve this (been looking for about two days now...).
I have a json string which looks like this:
[
  {
    "firstName": "dan",
    "lastName": "cohen",
    "email": "dann@gmail.com",
    "userName": "aaaddd",
    "password": "1cczzdddds",
    "agentCode": 0
  },
  {
    "firstName": "omer",
    "lastName": "sha",
    "email": "superomsha@gmail.com",
    "userName": "asdf",
    "password": "asdf",
    "agentCode": 1
  }
]
I am trying to read this json into Set<T> since I am going to use this function in order to read json files to set of other types. E.g, here I am trying to read into Set<Agent> while later on I'll try to read another file into Set<Passenger>.
This is the function which reads the json file:
public Set<T> read() {
    try {
        return new ObjectMapper().readValue(new File(this.fileName), new TypeReference<Set<Agent>>(){});
    }
    /* Errors handling */
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
The line Set<T> sample = new ObjectMapper().readValue(jsonString, new TypeReference<Set<Agent>>(){}); is responsible for deserialize the json string into a Set<Agent>.
But the moment I try to change it from:
Set<T> sample = new ObjectMapper().readValue(jsonString, new TypeReference<Set<Agent>>(){});
To:
Set<T> sample = new ObjectMapper().readValue(jsonString, new TypeReference<Set<T>>(){});
When trying to execute this code (which lies in the main function):
Set<Agent> agents = fileManagerAgent.read();
Iterator<Agent> it = agents.iterator();
while(it.hasNext()){
    System.out.println(it.next().getUserName());
}
I am getting an error saying:
Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class model.objects.Agent (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; model.objects.Agent is in unnamed module of loader 'app')
From what I have read, this happens because in run-time, something happens to T and the compiler addresses it as Object and so, it is deserializing the json into a LinkedHashMap.
Once it does that, I cannot access the Agent object inside the set.
This is what calls the function to read from a json file into Set<T> (which is also in the main function):
FileManager<Agent> fileManagerAgent = new FileManager<>("src/data/agents.json");
Set<Agent> agents = fileManagerAgent.read();
Also, this code is written using Jackson.
Any ideas on how to solve this?
If any more information is needed please inform me and I will update the question.
 
     
    