So right now I have a modular system for interacting with my Game()
object, which handles all data storage for the game. Right now I'm working on a system for saving all properties of the Game object into a json file. I already figured out to serialize the entire object using
save_json = json.dumps(G.__dict__, indent=4, default=lambda o: o.__dict__)
My problem now is deserializing and because my Game object is modular, I don't know what kind of nested objects will be inside the object, and I need a dynamic system for loading them. On top of everything, some properties of the Game object are dictionaries, some are lists, and some are just variables.
All solutions I've looked up end up hard coding the deserialization into the __init__
of the objects, which doesn't work for me because my system is modular.
CodePudding user response:
this is really what pickle
is designed for
save_data = pickle.dumps(g)
...
loaded_g = pickle.loads(save_data)
if however you are set on using json... maybe the following would work
g = Game()
g.__dict__.update(json.loads(save_game))