Java GSON和InstanceCreator问题

Java GSON和InstanceCreator问题,java,reflection,deserialization,gson,type-safety,Java,Reflection,Deserialization,Gson,Type Safety,我有以下POJO: public interface Shape { public double calcArea(); public double calcPerimeter(); } public class Rectangle implement Shape { // Various properties of a rectangle } public class Circle implements Shape { // Various properti

我有以下POJO:

public interface Shape {
    public double calcArea();
    public double calcPerimeter();
}

public class Rectangle implement Shape {
    // Various properties of a rectangle
}

public class Circle implements Shape {
    // Various properties of a circle
}

public class ShapeHolder {
    private List<Shape> shapes;

    // other stuff
}
抛出:

Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for interface    
net.myapp.Shape. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:167)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
    ... rest of stack trace ommitted for brevity
因此,我寻找并开始实现我自己的
ShapeInstanceCreator

public class ShapeInstanceCreator implements InstanceCreator<Shape> {
    @Override
    public Shape createInstance(Type type) {
        // TODO: ???
        return null;
    }
}
我能做什么

更新 根据@raffian的建议(他/她发布的链接),我实现了一个
接口适配器
,与链接中的一样(我没有做任何更改)。现在我得到了以下异常:

Exception in thread "main" com.google.gson.JsonParseException: no 'type' member found in what was expected to be an interface wrapper
    at net.myapp.InterfaceAdapter.get(InterfaceAdapter.java:39)
    at net.myapp.InterfaceAdapter.deserialize(InterfaceAdapter.java:23)
有什么想法吗?

你看了吗?看起来是实现InstanceCreators的一种非常干净的方法

我也在使用Gson,但由于序列化问题而切换到了。使用Flex,您不需要实例创建者,只要确保您的对象具有基于JavaBean规范的所有字段的getter/setter,就可以了:

 ShapeHolder sh = new ShapeHolder();
 sh.addShape(new Rectangle());
 sh.addShape(new Circle());
 JSONSerializer ser = new JSONSerializer();
 String json = ser.deepSerialize(sh);
 JSONDeserializer<ShapeHolder> der = new JSONDeserializer<ShapeHolder>();
 ShapeHolder sh2 = der.deserialize(json);
ShapeHolder sh=new ShapeHolder();
sh.addShape(新矩形());
sh.addShape(新圆());
JSONSerializer ser=新的JSONSerializer();
字符串json=ser.deepSerialize(sh);
JSONDeserializer der=新的JSONDeserializer();
ShapeHolder sh2=der.deserialize(json);
注意,在json中添加类名,如下面的序列化时间

{
    "HTTPStatus": "OK",
    "class": "com.XXX.YYY.HTTPViewResponse",
    "code": null,
    "outputContext": {
        "class": "com.XXX.YYY.ZZZ.OutputSuccessContext",
        "eligible": true
    }
}

因此JSON将非常麻烦;但是您不需要编写GSON中所需的
InstanceCreator

谢谢@raffian(+1)-我创建了一个
接口适配器
,如您发布的链接中所示。然后,我将其注册为
GsonBuilder
中的类型适配器(同样如示例所示)。现在我得到一个新的错误(请看我更新的问题)。你知道现在发生了什么吗?再次感谢!我不知道,抱歉,这就是我为什么改用flex的原因,为了避免这些废话!:-)是否有任何限制迫使您使用Gson?很抱歉延迟响应-我去睡觉了,只是给了FlexJSON一个旋转。多好的待遇啊!想象一下:即时可用的序列化/反序列化!你肯定让我改变了主意-谢谢!没错,它只是工作,欢迎来到黑暗面:-)检查这个:,它有一个基类而不是接口,但这就像你的问题。
 ShapeHolder sh = new ShapeHolder();
 sh.addShape(new Rectangle());
 sh.addShape(new Circle());
 JSONSerializer ser = new JSONSerializer();
 String json = ser.deepSerialize(sh);
 JSONDeserializer<ShapeHolder> der = new JSONDeserializer<ShapeHolder>();
 ShapeHolder sh2 = der.deserialize(json);
{
    "HTTPStatus": "OK",
    "class": "com.XXX.YYY.HTTPViewResponse",
    "code": null,
    "outputContext": {
        "class": "com.XXX.YYY.ZZZ.OutputSuccessContext",
        "eligible": true
    }
}