I have objects in my game that I want to be able to serialize to JSON. The problem is that attempting to serialize them while they have a pointer that points to an "owner" object results in a stack overflow.
For example:
public class Entity
{
Vector2 loc;
Item item;
public Entity(Vector2 loc)
{
this.loc = loc;
this.item = new Item(this.loc, this);
}
}
public class Item
{
Vector2 loc;
Entity owner;
public Item(Vector2 loc, Entity owner)
{
this.loc = loc;
this.owner = owner;
}
}
If I then call
Json json = new Json();
System.out.println(json.prettyprint(instanceOfEntity));
I get a stack overflow.
I realize that this is likely an architecture problem, but I'm not sure of the best way to go about solving it. What is the solution to this problem?