javagenericscompiler-warningsparameterizedjson-simple

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?


Solution

  • 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.