I suspect this is really easy and I’m just being stupid, but is there a way to serialize an object so that some fields are serialized as references rather than being recursively serialised themselves?
As an example:
class World // not serializable
{
// whole bunch of world data
}
class Quest implements Serializable
{
// some stuff that should be deep cloned when serialized
private Goal goal;
...etc...
// Reference to world, which should reference the same object when deserialized
private World world;
}
Basically I’m (ab)using serialization to do deep copies on rather large graphs of objects. But some of the objects reference the game world which there is only one of and should reference the same world when cloned.
I could mark world as transient, but then I end up with a null reference after serialization. And I guess I could provide a custom readObject that would re-populate the ‘world’ field, but due to it’s lack of scope I’d have to pull it out of a global somewhere, which is pretty hacky. If this was C I’d just write the address of the object into the stream and read it out and cast it to the right object type, but obviously that’s not an option here.
Anyone any suggestions?