/*
 * The MIT License
 *
 * Copyright 2014 Stephen Stafford <clothcat@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.clothcat.hpoolauto;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/** Functionality that wraps JSON objects and writes them to the filsystem
 *
 * @author Stephen Stafford <clothcat@gmail.com>
 */
public class JsonFileHelper {
    public static void writeToFile(JSONObject jo, String filename){
        // an array wrapper that we use to allow us to put a comment at the top
        // of the file to tell people not to edit
        JSONArray ja = new JSONArray();
        ja.put("This file is part of the HyperPool Automation Tool (HAT)");
        ja.put("© 2014 Stephen Stafford — all rights wossnamed");
        ja.put("DO NOT EDIT THIS FILE BY HAND.  BAD THINGS WILL HAPPEN!!");
        ja.put(jo);
        String s = ja.toString();
        s=prettify(s);
        System.out.println(s);
        
        String filepath = System.getProperty("user.home")+"/.Hyperpool/JSON/";
        File f = new File(filepath);
        f.mkdirs();
        f=new File(filepath+filename);
        try(PrintWriter p = new PrintWriter(f)){
            p.println(s);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JsonFileHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String prettify(String json){
        return new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(json));
    }
    
    public static void main(String[] args) throws JSONException {
        String s = "{\"poolAddress\":\"PADDA\",\"investments\":[{\"datestamp\":1415838826084,\"amount\":1000,\"fromAddress\":\"A\"},{\"datestamp\":1415838826084,\"amount\":2000,\"fromAddress\":\"B\"}],\"startTimestamp\":0,\"poolName\":\"Pool1\",\"status\":\"FILLING\"}";
        JSONObject jo = new JSONObject(s);
        writeToFile(jo, "foo");
    }
}

@@ -1154,17 +1154,16 @@
 RE.%0A */%0A
-%0A
 package 
@@ -1313,24 +1313,51 @@
 dException;%0A
+import java.io.FileReader;%0A
 import java.
@@ -1527,11 +1527,43 @@
 ct;%0A
-%0A/*
+import org.json.JSONTokener;%0A%0A/**%0A 
 * Fu
@@ -1717,16 +1717,833 @@
 elper %7B%0A
+%0A    private static final String FILEPATH = System.getProperty(%22user.home%22) + %22/.Hyperpool/JSON/%22;%0A%0A    public static JSONObject readFromFile(String filename) %7B%0A        try %7B%0A            File f = new File(FILEPATH + filename);%0A            JSONArray ja = new JSONArray(new JSONTokener(new FileReader(f)));%0A            // the actual JSONObject we want is element 3 since there are three %0A            // lines of comment that start the array%0A            return ja.getJSONObject(3);%0A        %7D catch (FileNotFoundException %7C JSONException ex) %7B%0A            Logger.getLogger(JsonFileHelper.class.getName()).log(Level.SEVERE, null, ex);%0A        %7D%0A        // if we get here someting is seriously broken!%0A        throw new RuntimeException(%22Reached the bottom of JsonFileHelper.readFromFile(String filename) method.%22);%0A    %7D%0A%0A
     publ
@@ -2600,16 +2600,17 @@
 ilename)
+ 
 %7B%0A      
@@ -3054,17 +3054,19 @@
        s
-=
+ = 
 prettify
@@ -3074,126 +3074,8 @@
 s);%0A
-        System.out.println(s);%0A        %0A        String filepath = System.getProperty(%22user.home%22)+%22/.Hyperpool/JSON/%22;
 %0A   
@@ -3097,24 +3097,24 @@
 ew File(
-filepath
+FILEPATH
 );%0A     
@@ -3137,17 +3137,19 @@
        f
-=
+ = 
 new File
@@ -3153,17 +3153,19 @@
 ile(
-filepath+
+FILEPATH + 
 file
@@ -3182,16 +3182,17 @@
      try
+ 
 (PrintWr
@@ -3219,16 +3219,17 @@
 iter(f))
+ 
 %7B%0A      
@@ -3446,16 +3446,17 @@
 ng json)
+ 
 %7B%0A      
@@ -3555,20 +3555,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -3893,16 +3893,39 @@
 NG%5C%22%7D%22;%0A
+        s=prettify(s);%0A
         
@@ -3987,17 +3987,259 @@
 jo, %22foo
-%22
+.json%22);%0A        // read the file back again!%0A        JSONObject jo2 = readFromFile(%22foo.json%22);%0A        System.out.println(s);%0A        String s2=prettify(jo2.toString());%0A        System.out.println(s2);%0A        System.out.println(s.equals(s2)
 );%0A    %7D

Add functionality to read json back from files