Add versioning to the serialized cards.
This commit is contained in:
parent
0171a56d6e
commit
3f8267e5ef
@ -34,6 +34,7 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectStreamException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -86,6 +87,7 @@ class CanvasView extends View implements PartGrid {
|
||||
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
||||
|
||||
if (!loadState()){
|
||||
// Sprinkle some elements around if no state is found
|
||||
parts.add(new Placeholder(this, 50, 50, 750, 500));
|
||||
parts.add(new ColorBox(this, 600, 1000, 700, 1100));
|
||||
parts.add(new RoundButton(this, 300, 1200, 80, 100));
|
||||
@ -102,7 +104,16 @@ class CanvasView extends View implements PartGrid {
|
||||
fileIn = new FileReader(file);
|
||||
char[] data = new char[(int) file.length()];
|
||||
fileIn.read(data);
|
||||
JSONArray jsonParts = new JSONArray(new String(data ));
|
||||
JSONObject state = new JSONObject(new String(data));
|
||||
JSONObject metadata = state.getJSONObject("metadata");
|
||||
int version = metadata.getInt("version");
|
||||
|
||||
if (version != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If version 0 -> known structure
|
||||
JSONArray jsonParts = state.getJSONArray("parts");
|
||||
for (int i = 0; i < jsonParts.length(); i++){
|
||||
connections.addAll(deserializeObject(jsonParts.getJSONObject(i)));
|
||||
}
|
||||
@ -349,7 +360,7 @@ class CanvasView extends View implements PartGrid {
|
||||
}
|
||||
|
||||
private byte[] serializeState() throws IOException {
|
||||
JSONArray partArray = new JSONArray();
|
||||
final JSONArray partArray = new JSONArray();
|
||||
for (Part part : parts) {
|
||||
JSONObject serializedPart = new JSONObject();
|
||||
try {
|
||||
@ -362,7 +373,15 @@ class CanvasView extends View implements PartGrid {
|
||||
partArray.put(serializedPart);
|
||||
}
|
||||
|
||||
return partArray.toString().getBytes("UTF-8");
|
||||
final JSONObject metadataObject = new JSONObject(new HashMap<String, Object>(){{
|
||||
put("version", 0);
|
||||
}});
|
||||
|
||||
final JSONObject state = new JSONObject(new HashMap<String, Object>(){{
|
||||
put("metadata", metadataObject);
|
||||
put("parts", partArray);
|
||||
}});
|
||||
return state.toString().getBytes("UTF-8");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
Loading…
Reference in New Issue
Block a user