I am using Gson to parse a json string into a Vector
which stores 1-2 different sub-classes of BackgroundShape
.
Example, BGRectangle
and BGTriangle
.
When I try to parse the string I get this error ..
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
Is there a better way to achieve this?
I'm trying to place sub classes into a Vector of their parents type.
This is the parent class ..
public abstract class Shape{
protected int textureIndex;
/**
* @param textureIndex
*/
public Shape(int textureIndex) {
this.textureIndex = textureIndex;
}
protected abstract void draw(GLAutoDrawable gLDrawable);
}
BackgroundShape
subclasses Shape
..
public abstract class BackgroundShape extends Shape{
private Vec3 position;
public BackgroundShape(Vec3 position, int textureIndex) {
super(textureIndex);
this.position = position;
}
}
BGRectangle
extends BackgroundShape
..
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) {
}
}
This is how I declare in json (only 1 BackgroundShape
for demo) ..
{
"bgShapes": [
{
"position": {
"x": 0.0,
"y": 50.0,
"z": -20.0
},
"textureSelection": 1,
"width": 450.0,
"height": 200.0
}
]
}
And my Java class representing this json string ..
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;
}
}