-
Notifications
You must be signed in to change notification settings - Fork 11
/
Issue.java
148 lines (138 loc) · 5.12 KB
/
Issue.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
145
146
147
148
package org.embulk.input.jira;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.StreamSupport;
public class Issue
{
private JsonObject flatten;
private final JsonObject json;
public Issue(final JsonObject original)
{
this.json = original;
}
public JsonElement getValue(final String path)
{
final List<String> keys = new ArrayList<>(Arrays.asList(path.split("\\.")));
return get(json, keys);
}
private JsonElement get(final JsonElement json, final List<String> keys)
{
if (json == null || json.isJsonNull()) {
return JsonNull.INSTANCE;
}
else if (keys.isEmpty() || (json.isJsonArray() && json.getAsJsonArray().size() == 0)) {
return json;
}
final String key = keys.get(0);
keys.remove(0);
if (json.isJsonArray()) {
final JsonArray arrays = new JsonArray();
for (final JsonElement elem : json.getAsJsonArray()) {
if (elem.isJsonObject()) {
arrays.add(elem.getAsJsonObject().get(key));
}
else {
arrays.add(elem);
}
}
return get(arrays, keys);
}
return get(json.getAsJsonObject().get(key), keys);
}
public synchronized JsonObject getFlatten()
{
if (flatten == null) {
flatten = new JsonObject();
manipulatingFlattenJson(json, "");
}
return flatten;
}
private void manipulatingFlattenJson(final JsonElement in, final String prefix)
{
if (in.isJsonObject()) {
final JsonObject obj = in.getAsJsonObject();
// NOTE: If you want to flatten JSON completely, please remove this if and addHeuristicValue
if (StringUtils.countMatches(prefix, ".") > 1) {
addHeuristicValue(obj, prefix);
return;
}
if (obj.entrySet().isEmpty()) {
flatten.add(prefix, obj);
}
else {
for (final Entry<String, JsonElement> entry : obj.entrySet()) {
final String key = entry.getKey();
final JsonElement value = entry.getValue();
manipulatingFlattenJson(value, appendPrefix(prefix, key));
}
}
}
else if (in.isJsonArray()) {
final JsonArray arrayObj = in.getAsJsonArray();
final boolean isAllJsonObject = arrayObj.size() > 0 && StreamSupport.stream(arrayObj.spliterator(), false).allMatch(JsonElement::isJsonObject);
if (isAllJsonObject) {
final Map<String, Integer> occurents = new HashMap<>();
for (final JsonElement element : arrayObj) {
final JsonObject obj = element.getAsJsonObject();
for (final Entry<String, JsonElement> entry : obj.entrySet()) {
final String key = entry.getKey();
occurents.merge(key, 1, Integer::sum);
}
}
final JsonObject newObj = new JsonObject();
for (final String key : occurents.keySet()) {
newObj.add(key, new JsonArray());
for (final JsonElement elem : arrayObj) {
newObj.get(key).getAsJsonArray().add(elem.getAsJsonObject().get(key));
}
}
manipulatingFlattenJson(newObj, prefix);
}
else {
flatten.add(prefix,
new JsonPrimitive("String value"));
}
}
else if (in.isJsonPrimitive()) {
flatten.add(prefix, in.getAsJsonPrimitive());
}
else {
flatten.add(prefix, JsonNull.INSTANCE);
}
}
private void addHeuristicValue(final JsonObject json, final String prefix)
{
final List<String> keys = Arrays.asList("name", "key", "id");
final List<String> heuristic = new ArrayList<>();
for (final Entry<String, JsonElement> entry : json.entrySet()) {
final String key = entry.getKey();
final JsonElement value = entry.getValue();
if (keys.contains(key) && !value.isJsonNull()) {
heuristic.add(key);
}
}
if (heuristic.isEmpty()) {
flatten.add(prefix, new JsonPrimitive(json.toString()));
}
else {
for (final String key : heuristic) {
final JsonElement value = json.get(key);
flatten.add(appendPrefix(prefix, key), value);
}
}
}
private String appendPrefix(final String prefix, final String key)
{
return prefix.isEmpty() ? key : prefix + "." + key;
}
}