5

The code below produces the following compiler warning:

JSONObject obj = new JSONObject();
obj.put("foo", "bar");

Compiler message:

unchecked call to put(K,V) as a member of the raw type java.util.HashMap

I populate the JSONObject with values via JSONObject.put() and then call obj.toString() to get the json out. How can I fix the warning above (I compile with -Werror).

The JSONObject is from the following library.

import org.json.simple.JSONObject;

Maven dependency:

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
2
  • what lib is that JSONObject from? Commented Nov 9, 2018 at 12:45
  • @DarrenForsythe updated - see above
    – s5s
    Commented Nov 9, 2018 at 12:46

4 Answers 4

9

The json-simple library is compiled with an old bytecode version: 46.0. This is Java 1.2. The JSONObject map extends java.util.HashMap and you are directly using the "put" method of java.util.HashMap

Generics were added in Java 5. Since Java 5, compiler encourages usage of generic types. This way, the compiler suggests, that you should upgrade your code to be more type safe.

In this case, the unsafe usage comes from a library and you have no control over it. I suggest to either search for a newer version of the library or to switch to another library.

Update: you can try following library as an alternative:

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>

Usage:

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.Collections;

public class Main {

    public static void main(String[] args) {
        JSONObject obj = new JSONObject();
        obj.put("hello", "world");
        obj.put("collection", new JSONArray(Arrays.asList(1, "two", Collections.singletonMap("three", 30))));
        System.out.println("obj.toString() = " + obj.toString());
    }
}
0
7

Although I won't call it the best solution but if you really want to use the same library without any warnings, then you can use

Map<String,String> jsonMap = new HashMap<>();
jsonMap.put("foo", "bar");
JSONObject obj = new JSONObject(jsonMap);

This will work without any warnings and it does what you need.

1
  • I didn't had to do JSONObject obj = new JSONObject(jsonMap); but your hint helped me :)
    – Alejandro
    Commented Nov 15, 2019 at 22:01
5

You are getting this warning because org.json.simple.JSONObject uses raw type collections internally.

public class JSONObject extends HashMap ...

If you want to get rid of this warning you can add the following annotation to your code.

@SuppressWarnings("unchecked")

Or even better you can try to use a library which supports generics...

0

You can get rid of that warning by explicitly casting it to the required type

 ((Map<String, String>)obj).put("foo", "bar")

but you'll at some point encounter another warning.

Type safety: Unchecked cast from JSONObject to Map<String,String>

This is because the class JSONObject extends from HashMap but doesn't specify the same generics as Hashmap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.