KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have this snippet of code: public abstract class Repository<Entity extends BaseObject> { ... public void readFromJson(){ String content = "JSON content here"; Gson gson = new Gson(); Type entityType = new TypeToken<JSONObject<Entity>>(){}.getType(); jsonObject = gson.fromJson(content, entityType); for (Entity ent : jsonObject.getEntities()) ; } } When I try to do the foreach my entities object is no longer of type Entity but LinkedHashMap and I get this exception: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.tranca.bookstore.domain.shared.BaseObject Here is the JSONObject class(created by me) public class JSONObject<Entity> { private List<Entity> entities = new ArrayList<Entity>(); private long lastId = -1; public List<Entity> getEntities() { return entities; } public void setEntities(List<Entity> entities) { this.entities = entities; } public long getLastId() { return lastId; } public void setLastId(long lastId) { this.lastId = lastId; } public void incrementLastId() { this.lastId++; } } maybe the base object is relevant so I will put the code here: public abstract class BaseObject implements Serializable { protected long id = (long) -1; protected int version = 0; protected BaseObject(){} public long getId() { return id; } public void setId(long id) { this.id = id; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } }
Tags (comma-separated)
Save Edits
Cancel