forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonQuery.java
More file actions
140 lines (118 loc) · 4.6 KB
/
Copy pathJsonQuery.java
File metadata and controls
140 lines (118 loc) · 4.6 KB
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
package org.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsonQuery {
private List<String> tokens;
private Map<String, Object> checkValue = null;
private final static Pattern pattern = Pattern.compile("\\[(.+?)\\]");
private Map<String, JsonQuery> subQueries = null;
public void setCheckValue(String key, Object value){
if (checkValue == null){
checkValue = new HashMap<>();
}
checkValue.put(key, value);
}
public JsonQuery(String query) {
subQueries = new HashMap<>();
tokens = new ArrayList<>();
init(query);
}
private JsonQuery(String condVar, JsonQuery jsonQuery) {
tokens = new ArrayList<>();
this.checkValue = jsonQuery.checkValue;
jsonQuery.checkValue=this.checkValue;
init(condVar);
}
private Object improvedGet(JSONObject json, String key) {
if (json == null) return null;
if (key.matches(".*\\[[0-9]+\\].*")) {
Matcher matcher = pattern.matcher(key);
matcher.find();
String id = matcher.group(1);
JSONArray arr = json.getJSONArray(key.substring(0, key.indexOf("[")));
return arr.get(Integer.parseInt(id) - 1);
} else if (key.matches(".*\\[[a-z=_A-Z/\\.0-9:]+\\].*")) {
Matcher matcher = pattern.matcher(key);
matcher.find();
String condition = matcher.group(1);
String[] conditions = condition.split("=");
String condVar = conditions[0];
String condVal = conditions[1];
if (condVal.startsWith(":")){
condVal=(String) checkValue.get(condVal);
}
JSONArray arr = json.getJSONArray(key.substring(0, key.indexOf("[")));
JsonQuery query = subQueries.get(condVar);
for (Object object : arr) {
JSONObject jObj = (JSONObject) object;
if (query.evaluatePath((JSONObject) object).equals(condVal)) {
return jObj;
}
}
return null;
} else
return json.opt(key);
}
private void init(String path) {
String[] seq = path.split("\\.(?![^\\[]*\\])");
for (String string : seq) {
tokens.add(unescape(string));
if (string.matches(".*\\[[a-z=_A-Z/\\.0-9:]+\\].*")){
Matcher matcher = pattern.matcher(string);
matcher.find();
String condition = matcher.group(1);
String[] conditions = condition.split("=");
String condVar = conditions[0];
JsonQuery subQuery = new JsonQuery(condVar,this);
subQueries.put(condVar, subQuery);
}
}
}
private String unescape(String token) {
return token.replace("~1", "/").replace("~0", "~").replace("\\\"", "\"").replace("\\\\", "\\");
}
private String getNextValidPath(Iterator<String> iter) {
String path = "";
while ("".equals(path)) {
path += iter.hasNext() ? iter.next() : "";
}
if (path != null && path.matches("^[0-9]+$")) {
path = "[" + path + "]";
}
return path;
}
public Object evaluatePath(JSONObject json) {
return evaluatePath(json, tokens.iterator());
}
private Object evaluatePath(JSONObject json, Iterator<String> iter) {
if (json == null) return null;
String path = getNextValidPath(iter);
if (!iter.hasNext()) {
return improvedGet(json, path);
} else {
JSONObject nextChild = (JSONObject) improvedGet(json, unescape(path));
if (nextChild instanceof JSONObject) {
return evaluatePath(nextChild, iter);
} else
return improvedGet(nextChild, unescape(path));
}
}
public static void main(String[] args) {
JSONObject js = new JSONObject("{ \"db1\":{ \"map1\":{ \"uri\":\"asdasdad\", \"content\":[ { \"key\":\"key1\", \"val\":{ \"no\":\"555222333\", \"name\":\"jeffrey\" } }, { \"key\":\"key2\", \"val\":{ \"no\":\"111777666\", \"name\":\"kate\" } } ] }, \"map2\":[ { \"key\":\"key1\", \"val\":\"value1\" }, { \"key\":\"key2\", \"val\":\"value2\" } ] }}");
JsonQuery q = new JsonQuery("db1.map1.content[key=key2].val.name");
System.out.println(q.evaluatePath(js));
JsonQuery q1 = new JsonQuery("db1.map1.content[val.name=:checkValue].key");
q1.setCheckValue(":checkValue", "kate");
System.out.println(q1.evaluatePath(js));
q1.setCheckValue(":checkValue", "jeffrey");
System.out.println(q1.evaluatePath(js));
JsonQuery q2 = new JsonQuery("db1.map2[2]");
System.out.println(q2.evaluatePath(js));
return;
}
}