package com.cuongnd.wpibannerweb.helper;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * Created by Cuong Nguyen on 6/2/2015.
 */
public class JSONSerializer {
    private static final String TAG = "JSONSerializer";

    public static JSONObject loadJSONFromFile(Context context, String fileName) {
        BufferedReader reader = null;
        try {
            InputStream in = context.openFileInput(fileName);
            reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder jsonString = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                jsonString.append(line);
            }
            return new JSONObject(jsonString.toString());
        } catch (IOException | JSONException e) {
            Utils.logError(TAG, e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                Utils.logError(TAG, e);
            }
        }
        return new JSONObject();
    }

    public static void saveJSONToFile(Context context, String fileName, JSONObject jsonObject) {
        Writer writer = null;
        try {
            OutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(jsonObject.toString());
        } catch (IOException e) {
            Utils.logError(TAG, e);
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                Utils.logError(TAG, e);
            }
        }
    }
}

@@ -238,32 +238,118 @@
 BufferedWriter;%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0A
 import java.io.I
@@ -739,113 +739,435 @@
     
-BufferedReader reader = null;%0A        try %7B%0A            InputStream in = context.openFileInput(fileName);
+return loadJSONFromFile(context, null, fileName);%0A    %7D%0A%0A    public static JSONObject loadJSONFromFile(Context context, File dir, String fileName) %7B%0A        BufferedReader reader = null;%0A        try %7B%0A            InputStream in;%0A            if (dir == null) %7B%0A                in = context.openFileInput(fileName);%0A            %7D%0A            else %7B%0A                in = new FileInputStream(new File(dir, fileName));%0A            %7D
 %0A   
@@ -1924,24 +1924,237 @@
 onObject) %7B%0A
+        saveJSONToFile(context, null, fileName, jsonObject);%0A    %7D%0A%0A    public static void saveJSONToFile(Context context, File dir, String fileName,%0A                                      JSONObject jsonObject) %7B%0A
         Writ
@@ -2210,16 +2210,68 @@
 tStream 
+out;%0A            if (dir == null) %7B%0A                
 out = co
@@ -2324,16 +2324,120 @@
 IVATE);%0A
+            %7D else %7B%0A                out = new FileOutputStream(new File(dir, fileName));%0A            %7D%0A
         

Upgrade JSONSerializer with methods that accept a directory as a parameter