package udo.storage;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

import com.google.gson.Gson;

/** File to test reading from and writing to between ArrayList and JSON
 */
public class JsonProcessor{

	public static void main(String args[]) {

	}
	
	/*
	 * Java Method to read JSON From File
	 */
	public static ArrayList<Task> readJson(String file) throws Exception {
		JSONParser parser = new JSONParser();
		ArrayList<Task> rtList = new ArrayList<Task>();
		try {
			System.out.println("Reading JSON file from Java program");
			FileReader fileReader = new FileReader(file);
			JSONArray json = (JSONArray) parser.parse(fileReader);
			for (int i=0; i<json.size(); i++) {
				Gson gson = new Gson();
				Task temp = gson.fromJson(json.get(i).toString(), Task.class);
				if (temp.getIndex()==i) {
					rtList.add(temp);
				}
				else {
					temp.setIndex(i);
				}
			}

		} catch (Exception ex) {
			throw ex;
		}
		return rtList;
	}

	/* Java Method to write JSON String to file
	 */
	public static boolean writeJson(String path, ArrayList<Task> myArr) {
		ArrayList<String> gsonArray = new ArrayList<String>();
		for (int i=0; i<myArr.size(); i++) {
			Gson gson = new Gson();
			gsonArray.add(gson.toJson(myArr.get(i)));
		}


		try {
			System.out.println("Writting JSON into file ...");
			System.out.println(gsonArray);
			FileWriter jsonFileWriter = new FileWriter(path);
			jsonFileWriter.write(gsonArray.toString());
			jsonFileWriter.flush();
			jsonFileWriter.close();
			System.out.println("Done");
			return true;

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}

@@ -596,32 +596,55 @@
 ask%3E();%0A%09%09try %7B%0A
+%09%09%09int countError = 0;%0A
 %09%09%09System.out.pr
@@ -1023,25 +1023,120 @@
 dex(i);%0A%09%09%09%09
-%7D
+%09rtList.add(temp);%0A%09%09%09%09%09countError++;%0A%09%09%09%09%7D%0A%09%09%09%09if (countError%3E0)%0A%09%09%09%09%09%09writeJson(file, rtList);
 %0A%09%09%09%7D%0A%0A%09%09%7D c

Update fix #67