Back to project page trivial-lists.
The source code is released under:
MIT License
If you think the Android project trivial-lists listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.hbabcock.triviallists; /*w ww . j a va2 s . c o m*/ 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; import android.util.Log; public class TrivialListJSONSerializer { private static final String TAG = "TrivialListJSONSerializer"; private Context mContext; public TrivialListJSONSerializer(Context c){ mContext = c; } public ArrayList<Category> loadCategories(String filename) throws IOException, JSONException { Log.i(TAG,"loadCategories"); ArrayList<Category> categories = new ArrayList<Category>(); BufferedReader reader = null; try{ InputStream in = mContext.openFileInput(filename); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder jsonString = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null){ jsonString.append(line); } JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue(); for (int i = 0; i < array.length(); i++){ categories.add(new Category(array.getJSONObject(i))); } } catch (FileNotFoundException e){ } finally { if (reader != null) reader.close(); } return categories; } public ArrayList<Item> loadItems(String filename) throws IOException, JSONException { Log.i(TAG,"loadItems"); ArrayList<Item> items = new ArrayList<Item>(); BufferedReader reader = null; try{ InputStream in = mContext.openFileInput(filename); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder jsonString = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null){ jsonString.append(line); } JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue(); for (int i = 0; i < array.length(); i++){ items.add(new Item(array.getJSONObject(i))); } } catch (FileNotFoundException e){ } finally { if (reader != null) reader.close(); } return items; } public void saveJSON(JSONArray array, String filename) throws IOException{ Writer writer = null; try { OutputStream out = mContext.openFileOutput(filename, Context.MODE_PRIVATE); writer = new OutputStreamWriter(out); writer.write(array.toString()); } finally { if (writer != null) writer.close(); } } public void saveCategories(ArrayList<Category> categories, String filename) throws JSONException, IOException { Log.i(TAG,"saveCategories"); JSONArray array = new JSONArray(); for (Category c : categories){ array.put(c.toJSON()); } saveJSON(array, filename); } public void saveItems(ArrayList<Item> items, String filename) throws JSONException, IOException { Log.i(TAG,"saveItems"); JSONArray array = new JSONArray(); for (Item i : items){ array.put(i.toJSON()); } saveJSON(array, filename); } }