2
votes

I want to create a JSON string piece by piece and by using org.json.simple.JSONArray and org.json.simple.JSONObject. Here's the code.

1. JSONObject config = new JSONObject();

2. JSONArray urls = new JSONArray();
3. urls.add("https://www.test1.com/v1");
4. urls.add("https://www.test1.com/v2");

5. config.put("name", "name-test1");
6. config.put("sipUrls", sipUrls);

There is Eclipse warning for line 2 and 3:

  • type safety: the method add(Object) belongs to the raw type ArrayList. references to generic type should be parameterized.

And warning for line 5 and 6:

  • type safety: the method put(object, object) belongs to the raw type HashMap. references to generic type HashMap should be parameterized.

How can I get rid of these warnings?

P.S.

The problem addressed here is different from this How to correctly use HashMap? because there's no way to create a JSONArray<String> or JSONObject<String>. JSONArray and JSONObject are not parameterizable.

1

1 Answers

1
votes

It appears that there's no way to parameterize org.json.simple.JSONArray and org.json.simple.JSONObject by its design. However, you can suppress these warnings by annotation:

@SuppressWarnings("unchecked")