Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON and Generics in Java - Type safety warning

I have some data stored in Java elements and I need to return it in a given format - JSONObject. While my implementation works fine, I'm still getting a warning message from eclipse (Version: Juno Service Release 2):

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

This is my code:

public interface Element {...}

public abstract class AbstractElement implements Element {...}

public final class Way extends AbstractElement {...}

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

public class WayToJsonConverter{
    ...
    public JSONObject wayToJson(){
        JSONObject obj = new JSONObject();
        obj.put("id",way.getId());
        ...
        return obj;
    }
    ...
}

The problematic line is : obj.put("id",way.getId());

Is there a way to solve this issue other then adding @SuppressWarnings("unchecked")?

like image 883
Itay Gal Avatar asked May 07 '13 09:05

Itay Gal


People also ask

How does the JVM handle generics in Java?

Thus, any changes to the syntax of the Java language, or to the JVM, had to avoid breaking older code. The way Java implements generics while satisfying this constraint is through the use of erasure. In general, here is how erasure works. When your Java code is compiled, all generic type information is removed (erased).

What is json JSON?

JSON stands for JavaScript Object Notation which is a lightweight text-based open standard designed which is easy for human-readable data interchange. In general, JSON is extended from JavaScript.

What is JSON-simple in Java?

json-simple is a simple java toolkit for JSON. json-simple library is fully compliance with JSON specification (RFC4627). json-simple uses Map and List internally for JSON processing. We can use json-simple for parsing JSON data as well as writing JSON to file.

How to read or write JSON data in Java programming language?

It is the method by which we can access means read or write JSON data in Java Programming Language. Here we simply use the json.simple library to access this feature through Java means we can encode or decode JSON Object using this json.simple library in Java Programming Language.


1 Answers

What is your JSONObject, does it inherit from HashMap? If does, the warn probably means that your should declare the JSONObject instance as follows:

JSONObject<String,Object> obj=new JSONObject<String,Object>();

Updated: Look at the definition of the JSONObject:

public class JSONObject extends HashMap

it extends HashMap but doesn't support parameter type, if its definition is

public class JSONObject<K,V> extends HashMap<K,V>

then we could write

JSONObject<String,Object> obj=new JSONObject<String,Object>();

and the put method will no longer generate the warning

like image 85
ltebean Avatar answered Sep 28 '22 06:09

ltebean