- Home /
Saving and loading different object types generated at runtime using JSON
Hello,
For my project I am generating lots of stars , planets, ships, monsters, and other such things at runtime, then saving them to JSON using NewtonSoft's JSON.NET. Each of these things is a pure data class that uses an interface called ISaveable. I save each object in my game to a JObject and then pass the string version of that object to my save game handler, which then adds them to a master save game JObject that gets written to a file in string format.
The problem is that the object type is lost when I save them. Currently I am getting around this by adding a "classtype" variable to each object and then looking for that when I deserialize, but that isn't very elegant, and it's kind of klunky.
Is there a way to save the object's type in JSON so that it just "knows" what it is? I haven't been able to find a way, but I am not the world's greatest programmer.