相关疑难解决方法(0)

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
}
Run Code Online (Sandbox Code Playgroud)

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

String shapeHolderAsStr = getString();
ShapeHolder holder = gson.fromJson(shapeHodlderAsStr, ShapeHolder.class);
Run Code Online (Sandbox Code Playgroud)

抛出:

Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for interface    
net.myapp.Shape. Register an …
Run Code Online (Sandbox Code Playgroud)

java reflection type-safety gson deserialization

11
推荐指数
1
解决办法
3万
查看次数

标签 统计

deserialization ×1

gson ×1

java ×1

reflection ×1

type-safety ×1