Java Gson类强制转换异常
我有一个类来保存一些json数据,如下所示:Java Gson类强制转换异常,java,generics,casting,gson,Java,Generics,Casting,Gson,我有一个类来保存一些json数据,如下所示: package org.swx.nursing.tools.configuration.data; import java.util.Set; import com.cerner.system.exception.Verifier; import com.cerner.system.exception.VerifyException; import com.google.common.collect.ImmutableSet; /** * C
package org.swx.nursing.tools.configuration.data;
import java.util.Set;
import com.cerner.system.exception.Verifier;
import com.cerner.system.exception.VerifyException;
import com.google.common.collect.ImmutableSet;
/**
* Class representing a simple {@link JsonData#identifier},
* {@link JsonData#data} format. This class can be used to
* persist application data for example in a Configuration file.
*
* @author SW029693
* @since v1.0
*/
public class JsonData <T>{
/**
* Represents a unique identifier
*/
private String identifier;
/**
* Represents the data pertaining to this {@link JsonData#identifier}
*/
private T data;
private static final Set<String> VALID_JSON_ID_TYPES = ImmutableSet.of("CONFIG","HOTKEYS");
public JsonData(String identifier, T data) {
super();
this.identifier = identifier;
this.data = data;
}
/**
* Getter for {@link JsonData#identifier}
* @return
*/
public String getIdentifier() {
return identifier;
}
/**
* Sets the {@link JsonData#identifier} to the given value
* @param identifier
* Represents a unique {@link JsonData#identifier}
* @throws VerifyException
* If the argument is {@code null} or {@code empty}
*/
public void setIdentifier(String identifier) throws VerifyException{
Verifier.verifyNotNull(identifier, "identifier : null");
Verifier.verifyNotEmpty(identifier,"identifier : empty");
this.identifier = identifier;
}
/**
* Getter for {@link JsonData}
* @return
*/
public T getData() {
return data;
}
/**
* Sets the {@link JsonData#data} to the given value
* @param identifier
* Represents a unique {@link JsonData#data}
* @throws VerifyException
* If the argument is {@code null}
*/
public void setData(T data) {
Verifier.verifyNotNull(data, "data : null");
this.data = data;
}
@Override
public String toString() {
return "JsonData [identifier=" + identifier + ", data=" + data + "]";
}
}
/**
* Helper method to read the config.json file
* @return
*/
private static JsonData<ConfigurationProperty> readconfigFile() {
Reader reader = null;
JsonData<ConfigurationProperty> data = null;
Gson gson = null;
try {
reader = new FileReader("./config.json");
gson = new GsonBuilder().create();
data = gson.fromJson(reader, JsonData.class);
System.out.println("yooo="+data.getData().getRunnableContext());
} catch (FileNotFoundException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage()); }
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage());
}
}
return data;
}
文件的编写方式如下:
{
"identifier": "CONFIG",
"data": {
"hotKey": "testKey",
"type": "APPLICATION",
"runnableContext": "testContext"
}
}
由于上述错误,我无法将写入文件的上述json的“数据”部分读取到JsonData对象中
请告知
谢谢请看
当您调用toJson(obj)时,Gson调用obj.getClass()以获取有关要序列化的字段的信息。类似地,通常可以在fromJson(json,MyClass.class)方法中传递MyClass.class对象。如果对象是非泛型类型,则此操作非常有效。但是,如果对象是泛型类型,那么由于Java类型擦除,泛型类型信息将丢失。下面是一个示例,说明了这一点:
class Foo<T> {
T value;
}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar
class-Foo{
T值;
}
Gson Gson=新的Gson();
Foo-Foo=新的Foo();
gson.toJson(foo);//可能无法正确序列化foo.value
gson.fromJson(json,foo.getClass());//无法将foo.value反序列化为Bar
上面的代码无法将值解释为类型Bar,因为Gson调用list.getClass()来获取其类信息,但此方法返回一个原始类Foo.class。这意味着Gson无法知道这是Foo类型的对象,而不仅仅是普通Foo
可以通过为泛型类型指定正确的参数化类型来解决此问题。您可以使用TypeToken类来实现这一点
Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);
Type fooType=new-TypeToken(){}.getType();
toJson(foo,fooType);
fromJson(json,fooType);
用于获取fooType的习惯用法实际上定义了一个匿名本地内部类,该类包含返回完全参数化类型的方法getType()
{
"identifier": "CONFIG",
"data": {
"hotKey": "testKey",
"type": "APPLICATION",
"runnableContext": "testContext"
}
}
class Foo<T> {
T value;
}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar
Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);