Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GSON 2.2.2无法序列化类型";?扩展someInterface“;_Java_Serialization_Gson - Fatal编程技术网

Java GSON 2.2.2无法序列化类型";?扩展someInterface“;

Java GSON 2.2.2无法序列化类型";?扩展someInterface“;,java,serialization,gson,Java,Serialization,Gson,Gson 2.2.2无法序列化Object1中的字段 public class Test { public static void main(String[] args){ Object1 o1 = new Object1(); List<Interface1> list1 = new ArrayList<Interface1>(); Interface1 f1 = new InterfaceImp();

Gson 2.2.2无法序列化Object1中的字段

public class Test {

    public static void  main(String[] args){
        Object1 o1 = new Object1();
        List<Interface1> list1 = new ArrayList<Interface1>();
        Interface1 f1 = new InterfaceImp();
        list1.add(f1);
        o1.field = list1;
        System.out.println(new Gson().toJson(o1));
    }
}
interface Interface1{}
class  InterfaceImp implements Interface1{
    public String s = "123";
}
class Object1 {
    public List<? extends Interface1> field ;
}
公共类测试{
公共静态void main(字符串[]args){
Object1 o1=新的Object1();
List list1=新的ArrayList();
Interface1 f1=新的InterfaceImp();
列表1.添加(f1);
o1.field=list1;
System.out.println(新的Gson().toJson(o1));
}
}
接口接口1{}
类InterfaceImp实现Interface1{
公共字符串s=“123”;
}
类对象1{

public List我认为您需要指定列表中使用的泛型类型

检查此链接

例如,要序列化列表,请执行以下操作:

Type type = new TypeToken<List<Interface1>>(){}.getType();
String s = new Gson().toJson(list1, type);
Type Type=new-TypeToken(){}.getType();
字符串s=new Gson().toJson(列表1,类型);
工作代码(已测试)

publicstaticvoidmain(字符串[]args){
Object1 o1=新的Object1();
List list1=新的ArrayList();
Interface1 f1=新的InterfaceImp();
列表1.添加(f1);
列表1.添加(f1);
o1.field=list1;
字符串s=getGsonWithAdapters().toJson(o1);
系统输出打印项次;
}
公共静态Gson getGsonWithAdapters(){
GsonBuilder gb=新的GsonBuilder();
gb.serializeNulls();
gb.registerTypeAdapter(Object1.class,新的CustomAdapter());
返回gb.create();
}
公共静态类CustomAdapter实现JsonSerializer{
@凌驾
公共JsonElement序列化(Object1对象,类型,
JsonSerializationContext(jsc){
JsonObject JsonObject=新的JsonObject();
类型t=newtypetoken(){}.getType();
添加(“field”,new Gson().toJsonTree(obj.field,t));
返回jsonObject;
}
}

我已经通过使用自定义适配器解决了这个问题,但是为什么不让“getRuntimeTypeIfMoreSpecific”方法在这种情况下返回实际类型呢?事实上我不知道,我有一个类似的问题,我也解决了自定义适配器的问题,它总是工作的,但需要手动添加一些代码行……无论如何,希望我的答案有帮助;)嗯,你希望它返回什么类型?你使用的是有界通配符,这意味着你没有类型。你…不能这样做。
Type type = new TypeToken<List<Interface1>>(){}.getType();
String s = new Gson().toJson(list1, type);
public static void main(String[] args) {
    Object1 o1 = new Object1();
    List<Interface1> list1 = new ArrayList<Interface1>();
    Interface1 f1 = new InterfaceImp();
    list1.add(f1);
    list1.add(f1);
    o1.field = list1;

    String s = getGsonWithAdapters().toJson(o1);
    System.out.println(s);
}

public static Gson getGsonWithAdapters() {
    GsonBuilder gb = new GsonBuilder();
    gb.serializeNulls();
    gb.registerTypeAdapter(Object1.class, new CustomAdapter());
    return gb.create();
}

public static class CustomAdapter implements JsonSerializer<Object1> {
    @Override
    public JsonElement serialize(Object1 obj, Type type,
            JsonSerializationContext jsc) {
        JsonObject jsonObject = new JsonObject();
        Type t = new TypeToken<List<Interface1>>() {}.getType();
        jsonObject.add("field", new Gson().toJsonTree(obj.field, t));
        return jsonObject;
    }
}