-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathJsonUtil.java
132 lines (110 loc) · 4.08 KB
/
JsonUtil.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
package me.earth.headlessmc.launcher.util;
import com.google.gson.*;
import lombok.experimental.UtilityClass;
import lombok.val;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@UtilityClass
public class JsonUtil {
public static final Gson PRETTY_PRINT =
new GsonBuilder().setPrettyPrinting().create();
public static final Gson GSON = new Gson();
/**
* @param file the file to parse.
* @return the JsonElement parsed from the file.
* @throws IOException if something goes wrong.
* @throws JsonIOException if something goes wrong.
* @throws JsonSyntaxException if something goes wrong.
*/
public static JsonElement fromFile(File file) throws IOException {
return fromInput(Files.newInputStream(file.toPath()));
}
public static JsonElement fromInput(InputStream stream) throws IOException {
try (InputStreamReader reader = new InputStreamReader(stream)) {
return JsonParser.parseReader(reader);
} catch (JsonParseException e) {
throw new IOException(e);
}
}
public static String getString(JsonElement jo, String... path) {
val result = getElement(jo, path);
// TODO: if JsonArray, iterate and concatenate
return result == null ? null : result.getAsString();
}
public static JsonObject getObject(JsonElement jo, String... path) {
val result = getElement(jo, path);
return result == null || !result.isJsonObject()
? null
: result.getAsJsonObject();
}
public static JsonObject getObjectOrNew(JsonElement jo, String... path) {
val result = getElement(jo, path);
return result == null || !result.isJsonObject()
? new JsonObject()
: result.getAsJsonObject();
}
public static JsonArray getArray(JsonElement jo, String... path) {
val result = getElement(jo, path);
return result == null || !result.isJsonArray()
? null
: result.getAsJsonArray();
}
public static JsonElement getElement(JsonElement jo, String... path) {
if (jo == null || !jo.isJsonObject()) {
return null;
}
JsonObject current = jo.getAsJsonObject();
for (int i = 0; i < path.length; i++) {
val element = current.get(path[i]);
if (element == null) {
return null;
} else if (!element.isJsonObject() && i < path.length - 1) {
return null;
} else if (i >= path.length - 1) {
return element;
}
current = element.getAsJsonObject();
}
return null;
}
public static Map<String, String> toStringMap(JsonObject json) {
return toMap(json, JsonElement::getAsString);
}
public static Map<String, Boolean> toBoolMap(JsonObject json) {
return toMap(json, JsonElement::getAsBoolean);
}
public static <T> Map<String, T> toMap(JsonObject json,
Function<JsonElement, T> mapper) {
return json.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> mapper.apply(e.getValue())));
}
public static <T> List<T> toList(JsonArray array,
Function<JsonElement, T> mapper) {
val result = new ArrayList<T>(array.size());
for (val element : array) {
result.add(mapper.apply(element));
}
return result;
}
public static JsonArray toArray(JsonElement element) {
if (element == null) {
return new JsonArray();
}
if (element.isJsonArray()) {
return element.getAsJsonArray();
}
val result = new JsonArray(1);
result.add(element);
return result;
}
}