JSON-Simple causes compiler warning "Type safety: The method put(Object, Object) belongs to the raw type HashMap."

I don't know if you still have this problem, but I think it will benefit others who came across this problem.

I came across this problem and after a while, I managed to get it fixed using a HashMap.

HashMap<String,Object> additionalDetails = new HashMap<String,Object>();
additionalDetails.put("showOppo", option.isShowOppo());
additionalDetails.put("showCont", option.isShowCont());
additionalDetails.put("contActionTaken", option.isConActionTaken());
additionalDetails.put("oppoActionTaken", option.isOppoActionTaken());

JSONObject additionalDetailsJSON = new JSONObject(additionalDetails);

If you don't know what type the hashmap will hold or if it holds multiple types, its safer to use Object. Otherwise use the proper type.

This solution works on json-simple 1.1.1 and Java 1.5 and up.


You are using JSON Simple. Its JSONObject is derived from HashMap but unfortunately doesn't use generic parameters (probably because it was created in pre-generic times). So the warnings you see are the same as in:

HashMap map = new HashMap();
map.put("showOppo", option.isShowOppo());

Unfortunately you can't avoid the warnings.

I would recommend to switch to another JSON library like GSON or Jackson.