java – GSON和InstanceCreator问题

我有以下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
}

我没有问题让GSON将ShapeHolder的一个实例序列化为JSON.但是,当我尝试将该JSON的String反序列化为ShapeHolder实例时,我收到错误:

String shapeHolderAsStr = getString();
ShapeHolder holder = gson.fromJson(shapeHodlderAsStr, ShapeHolder.class);

抛出:

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

所以我看了here并开始实现我自己的ShapeInstanceCreator:

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

但现在我卡住了:我只给了一个java.lang.reflect.Type,但我真的需要一个java.lang.Object所以我可以编写如下代码:

public class ShapeInstanceCreator implements InstanceCreator<Shape> {
    @Override
    public Shape createInstance(Type type) {
        Object obj = convertTypeToObject(type);

        if(obj instanceof Rectangle) {
            Rectangle r = (Rectangle)obj;
            return r;
        } else {
            Circle c = (Circle)obj;
            return c;
        }

        return null;
    }
}

我能做什么?

更新

根据@ raffian的建议(他/她发布的链接),我实现了一个完全类似链接中的InterfaceAdapter(我没有改变任何东西).现在我得到以下异常:

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)

有任何想法吗?

解决方法:

你看过this吗?看起来像是一个很好的干净方式来实现InstanceCreators.

我也在使用Gson,但由于序列化问题而改为FlexJSON.使用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);
上一篇:使用GSON将java.time.LocalDateTime(java 8)序列化为js Date的最佳实践


下一篇:android – Gson在发布的apk中反序列化空指针