-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTest.java
More file actions
66 lines (54 loc) · 2.1 KB
/
Copy pathTest.java
File metadata and controls
66 lines (54 loc) · 2.1 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
package com.javabaas.server;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
/**
* Created by Codi on 16/1/5.
*/
public class Test {
private static ObjectMapper objectMapper;
private Map root;
public static void main(String... strings) throws IOException {
new Test().test();
}
public void test() throws IOException {
System.out.println("aaa".split(";")[0]);
// objectMapper = new ObjectMapper();
// root = objectMapper.readValue("{\"c\":{\"$sub\":{\"where\":{\"d\":{\"$sub\":{\"where\":{\"name\":\"d\"},\"searchClass\":\"D\"}},\"e\":{\"$sub\":{\"where\":{\"name\":\"e\"},\"searchClass\":\"E\"}}},\"searchClass\":\"C\"}}}", Map.class);
// handleSub("", null, root);
}
private boolean handleSub(String field, Map<String, Object> parent, Map<String, Object> query) {
boolean flag = false;
boolean sub = false;
Set<Map.Entry<String, Object>> entries = query.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (key.equals("$sub")) {
flag = true;
}
if (entry.getValue() instanceof Map) {
sub = handleSub(key, query, (Map<String, Object>) value) || sub;
}
}
//子节点循环完毕
//返回后判断无子节点且节点名称为$sub则进行子查询处理
if (field.equals("$sub") && !sub) {
replaceSub(parent, query);
System.out.println(field);
try {
System.out.println(objectMapper.writeValueAsString(root));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return flag;
}
private void replaceSub(Map<String, Object> parent, Map<String, Object> sub) {
System.out.println("replace");
parent.remove("$sub");
parent.put("$in", "{}");
}
}