Hi folks.
Concise version: Is loading and saving games using serialization an effective solution on Android?
Full version:
I have spent the last few years of my hobby time writing an RPG game in Java. I now want to port it to Android, and have been reading up on Android development.
My lazy decision when writing the PC game was to implement save games using object serialization. All of a game’s data is under the object “Game”, and is serializable, so saving and loading games was easy. The risk is that a Java update will change the standard objects to the point that I cannot retrieve a saved game, and the player will have to start over. Given the nature of the game (it’s more rogue-like than anything else), this was not a problem.
Moving over to Android, I hope to preserve this approach rather than (finally) have to write toString and fromString methods, or figure out how to put all the data into SQLite. I may use the DB later, but to get started, I am trying to avoid taking on more work - redoing the UI is enough.
So, my question is, in your collective experience, is loading and saving games using serialization an effective solution on Android?
thanks,
Andrew
JSON is often fine, though not fast (so much string manipulation, especially string<->float) or small (floats again aren’t efficient, eg I wouldn’t use JSON for mesh data). It isn’t the best for object serialization for a few reasons. If you have a JSON array, you can’t embed type info, eg if your array is a LinkedList rather than an ArrayList. You’d have to wrap it in an object so you can annotate the concrete type to use for the array. Perhaps the biggest issue is JSON numbers are all floats, so you can have data loss with doubles or longs. libgdx handles this by treating JSON numbers as doubles and longs. libgdx’s automatic JSON<->object serialization works for relatively basic classes (eg it doesn’t allow using a subclass of the known class for collections because it doesn’t wrap the JSON array with an object to annotate the concrete type, as explained above), while Kryo’s works for all and is faster and much smaller. The downside is you can’t read or hand edit Kryo output, though I have not found this to be necessary for most usage.