aboutsummaryrefslogtreecommitdiff
path: root/src/main/persistance/JsonUtils.java
blob: 4e11a185897aa69eae4d3f7cf900b0893fc1c096 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package persistance;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JsonUtils {

    /**
     * REQUIRES: A valid filepath path, a writeable JSONObject json
     * EFFECTS: writes a String to a file
     */
    public static void writeToFile(JSONArray json, String path) {
        PrintWriter writer;
        try {
            writer = new PrintWriter(path);
            writer.print(json);
            writer.close();
        } catch (Exception e) {
            System.out.printf("Write to file failed with %s", e.toString());
        }
    }

    /**
     * REQUIRES: a path to a valid file containing JSONObject-serialized data
     * EFFECTS: reads a serialized String into a JSONObject
     */
    public static JSONArray readFromFile(String path) {
        String content;
        JSONArray deread;
        try {
            content = Files.readString(Paths.get(path));
            deread = new JSONArray(content);
        } catch (Exception e) {
            System.out.println("Read from file failed with %s");
            deread = new JSONArray();
        }
        return deread;
    }
}