Java 使用gson-fromjson解析子类

Java 使用gson-fromjson解析子类,java,json,inheritance,gson,Java,Json,Inheritance,Gson,我使用Gson将json字符串解析为向量,该向量存储背景形状的1-2个不同子类 例如,BGRectangle和BGTriangle 当我试图解析字符串时,我得到了这个错误 FileReader in = new FileReader("levels/level1.json"); BufferedReader br = new BufferedReader(in); LevelDefinition ld = new Gson().fromJson(br, LevelDefinition.class

我使用Gson将json字符串解析为
向量
,该向量存储
背景形状的1-2个不同子类

例如,
BGRectangle
BGTriangle

当我试图解析字符串时,我得到了这个错误

FileReader in = new FileReader("levels/level1.json");
BufferedReader br = new BufferedReader(in);
LevelDefinition ld = new Gson().fromJson(br, LevelDefinition.class);

Caused by: java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.resolve
有没有更好的方法来实现这一点?
我试着把子类放到它们父母类型的向量中。 这是父类

public abstract class Shape{

    protected int textureIndex;

    /**
     * @param textureIndex
     */
    public Shape(int textureIndex) {
        this.textureIndex = textureIndex;
    }

    protected abstract void draw(GLAutoDrawable gLDrawable);
}
BackgroundShape
子类
Shape

public abstract class BackgroundShape extends Shape{

    private Vec3 position;

    public BackgroundShape(Vec3 position, int textureIndex) {
        super(textureIndex);

        this.position = position;
    }
}
public class BGRectangle extends BackgroundShape{

    private float width;
    private float height;

    public BGRectangle (Vec3 position int textureIndex, float width, float height) { 
        super(position, textureIndex);

        this.width = width;
        this.height = height;
    };

    @Override
    public void draw(GLAutoDrawable gLDrawable) {
    }
}
bg矩形
扩展
背景形状

public abstract class BackgroundShape extends Shape{

    private Vec3 position;

    public BackgroundShape(Vec3 position, int textureIndex) {
        super(textureIndex);

        this.position = position;
    }
}
public class BGRectangle extends BackgroundShape{

    private float width;
    private float height;

    public BGRectangle (Vec3 position int textureIndex, float width, float height) { 
        super(position, textureIndex);

        this.width = width;
        this.height = height;
    };

    @Override
    public void draw(GLAutoDrawable gLDrawable) {
    }
}
这就是我在json中声明的方式(演示仅1
BackgroundShape

我的Java类表示这个json字符串

public class LevelDefinition {

    private Vector<BackgroundShape> bgShapes;

    /**
     * @return the bgShapes
     */
    public Vector<BackgroundShape> getBgShapes() {
        return bgShapes;
    }
    /**
     * @param bgShapes the bgShapes to set
     */
    public void setBgShapes(Vector<BackgroundShape> bgShapes) {
        this.bgShapes = bgShapes;
    }
}
公共类级别定义{
专用矢量图形;
/**
*@返回图形
*/
公共向量getBgShapes(){
返回图形;
}
/**
*@param bgShapes要设置的bgShapes
*/
公共空心收边形状(矢量形状){
this.bgShapes=bgShapes;
}
}
我觉得它很像

我解决这个问题的方法是编写自己的
JsonReader
JsonWriter
。您可以在我的回答中阅读更多关于如何做到这一点的内容:

基本上,在
TypeAdapter
中实现
read
方法来创建所需的向量类。如果这还不足以让你弄明白,让我知道,我会尝试添加更多的细节

您可以从以下内容开始:

public enum ShapeTypeAdapterFactory implements TypeAdapterFactory {
    INSTANCE;

    private static class ShapeTypeAdapter extends TypeAdapter<BackgroundShape> {
        @Override
        public final void write(JsonWriter out, BackgroundShape value) throws IOException {
            if(value == null) {
                out.nullValue();
                return;
            }

            // Your code goes here
        }

        @Override
        public final BackgroundShape read(JsonReader in) throws IOException {
            if(in.peek() == JsonToken.NULL) {
                in.nextNull();
                return null;
            }

            // Your code goes here      

            // Don't return null, return the new object
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        if(!BackgroundShape.class.isAssignableFrom(type.getRawType())) {
            return null;
        }

        return (TypeAdapter<T>) new ShapeTypeAdapter();
    }
}
另外,请阅读本Javadoc,以获得有关如何编写此示例中缺少的代码的更完整示例


我没有看到任何信息会告诉Gson需要创建BG矩形。我猜它在寻找放置宽度和高度的地方时会迷失方向。嗯,您提供的链接提到创建
TypeAdapterFactory
来处理
BackgroundShape
的多个子类。。你能帮忙吗?