summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/JsonManager.java
blob: 498933fe0a3910470a624e889ad945ff4f772610 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.redstoner.misc;

import com.redstoner.annotations.Version;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * This class provides simple JSON handling, like storing and loading from and to files.
 *
 * @author Pepich
 */
@Version (major = 1, minor = 0, revision = 2, compatible = -1)
public class JsonManager {
	private JsonManager() {}

	/**
	 * Loads a JSONObject from a file.
	 *
	 * @param source the file to load from.
	 *
	 * @return the JSONObject or null if the source does not contain a valid JSONObject.
	 */
	public static JSONObject getObject(File source) {
		if (!source.exists())
			return null;
		JSONParser parser = new JSONParser();
		try {
			FileReader reader    = new FileReader(source);
			Object     rawObject = parser.parse(reader);
			reader.close();
			JSONObject jsonObject = (JSONObject) rawObject;
			return jsonObject;
		} catch (IOException | ParseException e) {
		}
		return null;
	}

	/**
	 * Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br>
	 * Note that this operation will be run on a different thread and you do not need to take care of that yourself.
	 *
	 * @param object      the JSONObject to save.
	 * @param destination the file to write to.
	 */
	public static void save(JSONObject object, File destination) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				saveSync(object, destination);
			}
		});
		t.start();
	}

	/**
	 * Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br>
	 * Note that this operation will be run on the same thread that you are calling it from!
	 *
	 * @param object      the JSONObject to save.
	 * @param destination the file to write to.
	 */
	public static void saveSync(JSONObject object, File destination) {
		if (destination.exists())
			destination.delete();
		else if (!destination.getParentFile().exists())
			destination.getParentFile().mkdirs();
		try {
			destination.createNewFile();
			FileWriter writer      = new FileWriter(destination);
			String     json_string = object.toJSONString();
			writer.write(json_string);
			writer.flush();
			writer.close();
		} catch (IOException e) {
		}
	}

	/**
	 * Loads a JSONArray from a file.
	 *
	 * @param source the file to load from.
	 *
	 * @return the JSONArray or null if the source does not contain a valid JSONArray.
	 */
	public static JSONArray getArray(File source) {
		if (!source.exists())
			return null;
		JSONParser parser = new JSONParser();
		try {
			Object    rawObject = parser.parse(new FileReader(source));
			JSONArray jsonArray = (JSONArray) rawObject;
			return jsonArray;
		} catch (IOException | ParseException e) {
		}
		return null;
	}

	/**
	 * Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br>
	 * Note that this operation will be run on a different thread and you do not need to take care of that yourself.
	 *
	 * @param object      the JSONArray to save.
	 * @param destination the file to write to.
	 */
	public static void save(JSONArray array, File destination) {
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				saveSync(array, destination);
			}
		});
		t.start();
	}

	/**
	 * Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br>
	 * Note that this operation will be run on the same thread that you are calling it from!
	 *
	 * @param object      the JSONArray to save.
	 * @param destination the file to write to.
	 */
	public static void saveSync(JSONArray array, File destination) {
		if (destination.exists())
			destination.delete();
		else if (!destination.getParentFile().exists())
			destination.getParentFile().mkdirs();
		try {
			destination.createNewFile();
			FileWriter writer      = new FileWriter(destination);
			String     json_string = array.toJSONString();
			writer.write(json_string);
			writer.flush();
			writer.close();
		} catch (IOException e) {
		}
	}
}