2

The backstory:

I have a Location class for room objects. It contains an ArrayList for exits.

I have an Exit class for exits from locations, which store the direction of the exit and where it leads to.

I add exits like so:

p1.addExit(new Exit(Exit.NORTH, p2));
p2.addExit(new Exit(Exit.SOUTH, p1));

I serialize all of this information and save it in my save/load process. This all worked fine in a proof-of-concept with 5 locations.

The problem:

I actually have about 40 connected locations now, and get a stackoverflow on save/load because of all the connections.

I think the best way to deal with this at the moment would be to limit the recursion in the serialization. I cannot mark the exits transient because in the course of the game, they are added and removed. There are many variables that get changed for each location, so saving everything about them is the optimal solution, rather than doing a bunch of broken up saving/loading of individual object variables.

I have found several posts here on SO indicating that writing your own writeObject and readObject custom serialization handling is the best option to control the recursion. However, I freely admit I'm too much of a novice to know how to do this, and I have been unable to find any examples on limiting recursion that don't also involve the use of plugins that I'm not using.

Any guidance here would be greatly appreciated.

EDIT: This is not a dupe of the suggested dupe. I don't need help pinpointing the source of my stack overflow; I know what's causing it.

Nick Schroeder
  • 1,340
  • 1
  • 13
  • 17
  • My advice would be to not rely on the Java serialization mechanisms and implementation. Design the (file) format that fits the best, and write the necessary code for binding your model to it. – dotvav Aug 11 '15 at 15:32
  • 1
    possible duplicate of [StackOverflowError when serializing an object in Java](http://stackoverflow.com/questions/438875/stackoverflowerror-when-serializing-an-object-in-java) – dotvav Aug 11 '15 at 15:37
  • I don't think this is a dupe, I've read that post. I know exactly what is causing my stackoverflow, the accepted answer says "just write your own serialization", and the links in the other answers there are now dead. – Nick Schroeder Aug 11 '15 at 15:43
  • There are a lot of binding libraries that will help you "write your own serialization" to json, xml, csv, protobuf, flatbuffer etc... I find Java vanilla serialization archaic and awkward, but that's just my opinion :) – dotvav Aug 11 '15 at 15:49

0 Answers0