Java 中的 JSON 和泛型 - 类型安全警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16415436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON and Generics in Java - Type safety warning
提问by Itay Gal
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):
我有一些数据存储在 Java 元素中,我需要以给定的格式返回它 - JSONObject。虽然我的实现工作正常,但我仍然收到来自 eclipse 的警告消息(版本: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"
"类型安全:方法 put(Object, Object) 属于原始类型 HashMap。对泛型类型 HashMap 的引用应该被参数化"
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());
有问题的线路是: obj.put("id",way.getId());
Is there a way to solve this issue other then adding @SuppressWarnings("unchecked")
?
除了添加之外,有没有办法解决这个问题@SuppressWarnings("unchecked")
?
采纳答案by ltebean
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 是什么,它是从 HashMap 继承的吗?如果是,警告可能意味着您应该按如下方式声明 JSONObject 实例:
JSONObject<String,Object> obj=new JSONObject<String,Object>();
Updated: Look at the definition of the JSONObject:
更新:查看 JSONObject 的定义:
public class JSONObject extends HashMap
it extends HashMap but doesn't support parameter type, if its definition is
它扩展了 HashMap 但不支持参数类型,如果它的定义是
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
并且 put 方法将不再生成警告
回答by David Conrad
If you can't switch to another library or modify the code of this library to make it generic, the only other option would be to write a wrapper around this library which uses it, and properly supports generics.
如果您无法切换到另一个库或修改该库的代码以使其通用,则唯一的其他选择是围绕使用它的该库编写一个包装器,并正确支持泛型。
So you would have your own JSONObject
class which would contain an org.json.simple.JSONObject
, would extend HashMap<String, Object>
and implement Map<String, Object>
, and would contain forwarding methods for all the methods of org.json.simple.JSONObject
.
因此,您将拥有自己的JSONObject
类,该类将包含org.json.simple.JSONObject
、扩展HashMap<String, Object>
和实现Map<String, Object>
,并且将包含 的所有方法的转发方法org.json.simple.JSONObject
。
You would still have to put @SuppressWarnings("unchecked")
in this class, but it would be limited to this class, and all the rest of your code could be free of generic warnings or the suppression of them.
您仍然需要放入@SuppressWarnings("unchecked")
这个类,但它仅限于这个类,并且您的所有其余代码都可以没有通用警告或对它们的抑制。
回答by squarephoenix
FYI org.codehaus.jettison.json.JSONObject
will not cause this warning. When using codehaus' JSONObject, you also have the ability to catch parsing errors via org.codehaus.jettison.json.JSONException
. See https://github.com/codehaus/jettisonfor details.
仅供参考org.codehaus.jettison.json.JSONObject
不会导致此警告。使用 codehaus 的 JSONObject 时,您还可以通过org.codehaus.jettison.json.JSONException
. 有关详细信息,请参阅https://github.com/codehaus/jettison。
回答by Kranthi
You could create a map object and then do an explicit cast to JSONObject
您可以创建一个地图对象,然后对 JSONObject 进行显式转换
Map<String, String> obj = new HashMap<String, String>();
obj.put("id",way.getId());
JSONObject jsonObj = (JSONObject) obj;
But note that this will restrict you only include "Strings" in your JSON. and you will see compile errors if you put another data structure. Say an array.
但请注意,这将限制您仅在 JSON 中包含“字符串”。如果您放置另一个数据结构,您将看到编译错误。说一个数组。
回答by asherbar
Another option is to initialize the JSONObject
with a (parameterized) Map<String, Object>
. That way, a value can be of any valid JSON type, and you avoid the unchecked
warning. E.g.:
另一种选择是JSONObject
用 a (parameterized)初始化Map<String, Object>
。这样,值可以是任何有效的 JSON 类型,您就可以避免出现unchecked
警告。例如:
public class WayToJsonConverter{
...
public JSONObject wayToJson(){
Map<String, Object> forJsonObj = new HashMap<>();
forJsonObj.put("id",way.getId()); // No warning, hurray!
forJsonObj.put("integer", 14);
forJsonObj.put("floating", 1.4);
JSONObject obj = new JSONObject(forJsonObj);
...
return obj;
}
...
}
回答by Dakshin Rajavel
public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware
公共类 JSONObject 扩展 HashMap 实现 Map、JSONAware、JSONStreamAware
But does not have type parameter in class definition, The only option you have is to add the @SuppressWarnings("unchecked")
但是在类定义中没有类型参数,您唯一的选择是添加 @SuppressWarnings("unchecked")