Java 为什么是杰克逊,格森。。。不使用泛型,而是将类对象传递给反序列化对象
例如: 为什么不: Foo result=mapper.readValuejsonStr 而不是 Foo result=mapper.readValuejsonStr,Foo.class Java泛型有什么限制阻止他们使用它?简短回答 这是Java泛型的限制 限制是无法从类型变量中选择。因此,不能调用t.class来获取下一个方法所需的t的class对象 长话短说 请看一下实现Java 为什么是杰克逊,格森。。。不使用泛型,而是将类对象传递给反序列化对象,java,generics,Java,Generics,例如: 为什么不: Foo result=mapper.readValuejsonStr 而不是 Foo result=mapper.readValuejsonStr,Foo.class Java泛型有什么限制阻止他们使用它?简短回答 这是Java泛型的限制 限制是无法从类型变量中选择。因此,不能调用t.class来获取下一个方法所需的t的class对象 长话短说 请看一下实现 public <T> T readValue(String content, Class<T>
public <T> T readValue(String content, Class<T> valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType));
}
public JavaType constructType(Type type) {
return _fromAny(null, type, EMPTY_BINDINGS);
}
这在泛型中是不可能的
实例
我试过以下方法
private <T> T readValue(String content)
{
return constructType(T.class);
}
private JavaType constructType(Type type)
{
//construct type somehow
}
这将成功编译
也看到
你说什么限制?有限制@RomanC限制是无法从类型变量中选择。因此,您不能调用t.class来获取下一个方法所需的类。
private <T> T readValue(String content)
{
return constructType(T.class);
}
private JavaType constructType(Type type)
{
//construct type somehow
}
private <T> T readValue(String content, Class<T> clazz)
{
return constructType(clazz);
}
private JavaType constructType(Type type)
{
//construct type somehow
}