/
JsonConfiguration.java
144 lines (126 loc) · 5.3 KB
/
JsonConfiguration.java
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.onarandombox.multiverseinventories.util;
import com.dumptruckman.minecraft.util.Logging;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.json.simple.JSONValue;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
public class JsonConfiguration extends FileConfiguration {
protected static final String BLANK_CONFIG = "{}\n";
//private static final JSONParser JSON_PARSER = ;
@Override
public String saveToString() {
String dump = JSONValue.toJSONString(buildMap(getValues(false)));
if (dump.equals(BLANK_CONFIG)) {
dump = "";
}
return dump;
}
private Map<String, Object> buildMap(final Map<String, Object> map) {
final Map<String, Object> result = new LinkedHashMap<String, Object>();
try {
for (final Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof ConfigurationSection) {
result.put(entry.getKey(), buildMap(((ConfigurationSection) entry.getValue()).getValues(false)));
} else if (entry.getValue() instanceof ConfigurationSerializable) {
ConfigurationSerializable serializable = (ConfigurationSerializable) entry.getValue();
Map<String, Object> values = new LinkedHashMap<String, Object>();
values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
values.putAll(serializable.serialize());
result.put(entry.getKey(), buildMap(values));
} else {
result.put(entry.getKey(), entry.getValue());
}
}
} catch (Exception e) {
Logging.getLogger().log(Level.WARNING, "Error while building configuration map.", e);
}
return result;
}
@Override
public void loadFromString(String contents) throws InvalidConfigurationException {
Validate.notNull(contents, "Contents cannot be null");
if (contents.isEmpty()) {
return;
}
Map<?, ?> input;
try {
input = (Map<?, ?>) new JSONParser(JSONParser.USE_INTEGER_STORAGE).parse(contents);
} catch (ParseException e) {
throw new InvalidConfigurationException("Invalid JSON detected.");
} catch (ClassCastException e) {
throw new InvalidConfigurationException("Top level is not a Map.");
}
if (input != null) {
convertMapsToSections(input, this);
}
}
protected void convertMapsToSections(Map<?, ?> input, ConfigurationSection section) {
Object result = dealWithSerializedObjects(input);
if (result instanceof Map) {
input = (Map<?, ?>) result;
for (Map.Entry<?, ?> entry : input.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
if (value instanceof Map) {
convertMapsToSections((Map<?, ?>) value, section.createSection(key));
} else {
section.set(key, value);
}
}
} else {
section.set("", result);
}
}
protected Object dealWithSerializedObjects(final Map<?, ?> input) {
final Map<String, Object> output = new LinkedHashMap<String, Object>();
for (final Map.Entry<?, ?> e : input.entrySet()) {
if (e.getValue() instanceof Map) {
output.put(e.getKey().toString(), dealWithSerializedObjects((Map<?, ?>) e.getValue()));
} else {
output.put(e.getKey().toString(), e.getValue());
}
}
if (output.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
try {
return ConfigurationSerialization.deserializeObject(output);
} catch (IllegalArgumentException ex) {
throw new YAMLException("Could not deserialize object", ex);
}
}
return output;
}
@Override
protected String buildHeader() {
return "";
}
@Override
public JsonConfigurationOptions options() {
if (options == null) {
options = new JsonConfigurationOptions(this);
}
return (JsonConfigurationOptions) options;
}
public JsonConfiguration(final File file) {
try {
load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file , ex);
}
}
}