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
|
package com.redstoner.misc.mysql;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.redstoner.misc.Main;
public class JSONManager
{
public static Map<Serializable, Serializable> getConfiguration(String fileName)
{
File file = new File(Main.plugin.getDataFolder(), fileName);
if (!file.exists())
{
try
{
PrintWriter writer = new PrintWriter(file.getAbsolutePath(), "UTF-8");
writer.println("{}");
writer.close();
}
catch (FileNotFoundException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
try
{
return loadMap(file);
}
catch (IOException | ParseException e)
{
e.printStackTrace();
return null;
}
}
public static void saveConfiguration(Map<Serializable, Serializable> config, String fileName)
{
try
{
saveMap(new File(Main.plugin.getDataFolder(), fileName), config);
}
catch (IOException e)
{
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void saveList(File file, List<Serializable> entries) throws IOException
{
JSONArray array = new JSONArray();
array.addAll(entries);
FileWriter writer = new FileWriter(file);
writer.write(array.toJSONString());
writer.close();
}
public static List<Serializable> loadList(File file) throws IOException, ParseException
{
FileReader read = new FileReader(file);
List<Serializable> entries = new ArrayList<>();
JSONArray array = (JSONArray) new JSONParser().parse(read);
for (Object o : array)
{
entries.add((Serializable) o);
}
return entries;
}
@SuppressWarnings("unchecked")
public static void saveMap(File file, Map<Serializable, Serializable> entries) throws IOException
{
JSONObject map = new JSONObject();
map.putAll(entries);
FileWriter writer = new FileWriter(file);
writer.write(map.toJSONString());
writer.close();
}
public static Map<Serializable, Serializable> loadMap(File file) throws IOException, ParseException
{
FileReader reader = new FileReader(file);
JSONObject map = (JSONObject) new JSONParser().parse(reader);
Map<Serializable, Serializable> entries = new HashMap<>();
for (Object o : map.keySet())
{
entries.put((Serializable) o, (Serializable) map.get(o));
}
return entries;
}
}
|