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")
?
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).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With