Android Open Source - CriminalIntent Criminal Intent J S O N Serializer






From Project

Back to project page CriminalIntent.

License

The source code is released under:

MIT License

If you think the Android project CriminalIntent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ambergleam.criminalintent.helper;
// ww w. ja  v  a  2s  . 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 com.ambergleam.criminalintent.model.Crime;

public class CriminalIntentJSONSerializer {

  private Context mContext;
  private String mFilename;
  
  public CriminalIntentJSONSerializer(Context c, String f) {
    mContext = c;
    mFilename = f;
  }
  
  public ArrayList<Crime> loadCrimes() throws IOException, JSONException {
    ArrayList<Crime> crimes = new ArrayList<Crime>();
    BufferedReader reader = null;
    try {
      // Open and read the file into a StirnbBuilder
      InputStream in = mContext.openFileInput(mFilename);
      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;
  }
  
  public void saveCrimes(ArrayList<Crime> 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 = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE);
      writer = new OutputStreamWriter(out);
      writer.write(array.toString());
    } finally {
      if (writer != null) {
        writer.close();
      }
    }
  }
  
}




Java Source Code List

com.ambergleam.criminalintent.controller.CrimeFragment.java
com.ambergleam.criminalintent.controller.CrimeListActivity.java
com.ambergleam.criminalintent.controller.CrimeListFragment.java
com.ambergleam.criminalintent.controller.CrimePagerActivity.java
com.ambergleam.criminalintent.controller.DatePickerFragment.java
com.ambergleam.criminalintent.helper.CriminalIntentJSONSerializer.java
com.ambergleam.criminalintent.helper.SingleFragmentActivity.java
com.ambergleam.criminalintent.model.CrimeLab.java
com.ambergleam.criminalintent.model.Crime.java