package com.example.criminalintent; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONTokener; import android.content.Context; public class JSONSerializer { private Context context; private String filename; public JSONSerializer(Context context, String filename) { this.context = context; this.filename = filename; } public void saveCrimes(ArrayList crimes) throws JSONException, IOException { // Build an array in JSON JSONArray array = new JSONArray(); for (Crime c : crimes) array.put(c.toJSON()); // Write the file to disk Writer writer = null; try { OutputStream out = context.openFileOutput(filename, Context.MODE_PRIVATE); writer = new OutputStreamWriter(out); writer.write(array.toString()); } finally { if (writer != null) writer.close(); } } public ArrayList loadCrimes() throws IOException, JSONException { ArrayList crimes = new ArrayList(); BufferedReader reader = null; try { // Open and read the file into a StringBuilder InputStream in = context.openFileInput(filename); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder jsonString = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { // Line breaks are omitted and irrelevant jsonString.append(line); } // Parse the JSON using JSONTokener JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue(); // Build the array of crimes from JSONObjects for (int i = 0; i < array.length(); i ++) { crimes.add(new Crime(array.getJSONObject(i))); } } catch (FileNotFoundException e) { // Ignore this one; it happens when starting fresh } finally { if (reader != null) reader.close(); } return crimes; } }