Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I Just came across a situation where I need to put the data in the JSONObject, while doing that I received a warning from the compiler regarding.

Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap should be parameterized.

I tried to parameterize the JSONObject but it gave me the error.

I am using following code where option is a Object.

JSONObject additionalDetails = new JSONObject();
additionalDetails.put("showOppo", option.isShowOppo());
additionalDetails.put("showCont", option.isShowCont());
additionalDetails.put("contActionTaken", option.isConActionTaken());
additionalDetails.put("oppoActionTaken", option.isOppoActionTaken());

How is this caused and how can I solve it?

like image 402
vaibhav Avatar asked Feb 17 '16 09:02

vaibhav


1 Answers

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.

like image 115
Ashwin Avatar answered Sep 28 '22 02:09

Ashwin