3

I'm trying to serialize an object graph in JSON.NET in which a Galaxy contains StarSystems, a StarSystem contains Planets, a Planet contains a Colony, a Colony has an Owner, and the Galaxy also has a list of Empires, each of which has a list of explored StarSystems. So there's a circular reference between StarSystem, Planet, and Empire.

What I don't understand is the behavior I get when I serialize the object graph. Instead of throwing an error when I have the reference loop handling set to the default, and trying to serialize everything when I have it set to serialize mode, it seems to be serializing a few links in the chain, then giving up and setting arbitrary values (in my case the explored star systems) to null. Naturally this is causing crashes elsewhere in the application when it looks for star systems but finds only nulls!

Could this have something to do with the fact that some of the links in the chain are not direct references, but collections? E.g. an empire does not have one explored star system, but a list of them.

So how can I make JSON.NET serialize everything? Do I need to eliminate the circular references and just pass around ID's of objects instead of actual object references in my object model? I really don't want to do that just for serialization purposes!

ekolis
  • 6,270
  • 12
  • 50
  • 101
  • 1
    Do you have some code? – Matt Burland May 10 '13 at 00:32
  • JSON.NET should have no problem serializing any object graph (or restoring it correctly). Most likely your code is broken (e.g. by doing unexpected things in property setters). Post some code.. – Morten Mertner May 10 '13 at 00:43
  • Not sure what code would be useful - the code when I call the serializer? Something else? (If I don't save and reload the game, and just work in-memory, everything looks fine.) – ekolis May 10 '13 at 07:13
  • In case it's helpful, here is the code where I serialize and deserialize the galaxy: http://pastebin.com/ciAexjKJ – ekolis May 10 '13 at 07:55
  • Also, the star systems are not completely deleted from the object graph - they still exist within the galaxy, but they are no longer referenced from the empire's list of explored systems. – ekolis May 10 '13 at 18:08

2 Answers2

1

I came up with a workaround. Instead of each empire storing a list of explored star systems, each star system will store a list of empires that have explored it. This way, no circular references!

ekolis
  • 6,270
  • 12
  • 50
  • 101
0

Not sure how I did this, but it looks like now I'm just getting null added to the list of explored star systems, instead of replacing the list of explored star systems, so I can just filter it out. Sorry I don't have any clue what I did to fix this!

ekolis
  • 6,270
  • 12
  • 50
  • 101
  • And now I'm getting just plain nulls again. It's completely random. – ekolis May 10 '13 at 07:40
  • Actually not random. It seems to have trouble when the star system is referred to via a $ref from the empire in the JSON, rather than contained within the empire. – ekolis May 10 '13 at 07:43