I use the libGdx Json functionality to save my game state to JSON;
public class GameInstance
//Some fields, constructors, utilities
public void saveState(GameState state)
String save = json.prettyPrint(state);
//Save to the file using FileHandle
public void loadState(FileHandle file)
String stateText = file.toString();
state = json.fromJson(stateText);
In my GameState I store the version of my app;
public class GameState
public final String versionID = "0.1.1";
//game access functions
Before I access the modified field, I check to see if the versionID is correct and will allow me to read it. This allows me to write simple fallback mechanics that help provide as much of the new functionality as possible.
I keep a field-changes.txt file that I use to log changes that I make in my fields for each class. I almost never remove a field entirely for compatability reasons. When I do I try to add functions that can work out a suitable replacement value.
Now, without libGdx you can just use the Jackson JSON API(link to tutorial on how to use Jackson). The details can be found in this Stack Overflow question. In short, there is apparently an annotation that you can use that has this purpose in mind specifically.
The accepted answer says you should do this;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo
Also check out the next best answer. Either way, the idea is still the same; to store the version of your GameState in each saved state and refactor the code to use the old data in alternative ways that makes the newest functionality possible.
Note: I only use libGdx and its Json library. Alternatively, if you wanted to use its Json library you could go and get the Json sourcecode. You would also need to copy the libGdx-specific collections and primitive collections to your project and fix the setup. You would have to do some minor editing to make it work.