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

6

18

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?

Klipspringer answered 17/2, 2016 at 9:48 Comment(0)
L
14

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.

Loyalty answered 17/2, 2016 at 9:55 Comment(1)
the answer below shows how you can avoid the warning.Rahman
S
38

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.

Swim answered 17/10, 2016 at 12:59 Comment(5)
When I try create a JSONObject passing HashMap in the constructor I get following - The constructor JSONObject(HashMap<String,Object>) is undefined.Dote
this should be the accepted answer in my opinion! Thanks!Rahman
yes this should have been the accepted answer, works like a charm and this seems correct way to do it.Kesler
JSONObject does not seem to have a constructor that takes an object, just a plain no-arg constructor.Vivisection
@KeithTyler Make sure the versions match at least (json-simple 1.1.1 and Java 1.5 and up). I have not tested on other versions of json-simple, just v1.1.1.Swim
L
14

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.

Loyalty answered 17/2, 2016 at 9:55 Comment(1)
the answer below shows how you can avoid the warning.Rahman
T
1

I recently face this problem.There are two libraries for JSONObject which is creating confusion for you You have used wrong json jar that is json-simple-1.1.jar, package that you imported is org.json.simple.JSONObject, use java-json.jar and import org.json.JSONObject download jar from http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

Tamayo answered 6/6, 2017 at 11:59 Comment(1)
i am starting to feel that people should be directed away from jsonsimple. One big issue is the poor package naming that collides with the actual json.org java implementation. Makes it impossible / daunting to use both libraries. But it is just not robust or clean.Vivisection
A
1

to avoid the warning and to better work I change it like that (when JSONObject additionalDetails)

import com.jayway.jsonpath.JsonPath;

 JsonPath.parse(additionalDetails).set(fieldPath, Value);
Athalia answered 26/10, 2020 at 9:37 Comment(0)
E
0

If option.isShowOppo() is returning boolean value. Try Boolean value= option.isShowOppo(); and then additionalDetails.put("showOppo",value);

Elwira answered 17/2, 2016 at 10:7 Comment(0)
M
0

Use this json implementation instead - it 100% resolves the issue! Note: use method toString() instead of toJSONString()

import org.json.JSONObject;

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20231013</version>
</dependency>
Maus answered 25/10, 2023 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.