-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathDiffJSONVisitor.java
More file actions
72 lines (61 loc) · 2.61 KB
/
DiffJSONVisitor.java
File metadata and controls
72 lines (61 loc) · 2.61 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
package de.danielbechler.diff.node;
import de.danielbechler.diff.path.NodePath;
import de.danielbechler.diff.selector.BeanPropertyElementSelector;
import de.danielbechler.diff.selector.ElementSelector;
import de.danielbechler.util.Strings;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This visitor can take the differencies and output the value from the modified object.
* You can either get the result as a {@link Map} or get it as a JSON-string.
* @author Patrick Fust (patrickfust)
*/
public class DiffJSONVisitor extends PrintingVisitor {
private final Map<String, Object> messages = new LinkedHashMap<String, Object>();
public DiffJSONVisitor(Object working, Object base) {
super(working, base);
}
@Override
protected String differenceToString(DiffNode node, Object base, Object modified) {
String text = Strings.toSingleLineString(node.canonicalGet(modified));
NodePath nodePath = node.getPath();
getMapFromPath(nodePath).put(getLastName(nodePath), text);
return text;
}
public Map<String, Object> getMessagesAsMap() {
return messages;
}
public String getAsJSON() throws IOException {
return new ObjectMapper().writeValueAsString(messages);
}
@Override
protected void print(final String text) {
}
private Map<String, Object> getMapFromPath(NodePath path) {
Map<String, Object> resultMap = messages;
List<ElementSelector> elementSelectors = path.getElementSelectors();
int idx = 0;
for (ElementSelector elementSelector : elementSelectors) {
if (elementSelector instanceof BeanPropertyElementSelector) {
BeanPropertyElementSelector beanPropertyElementSelector = (BeanPropertyElementSelector) elementSelector;
if (idx != elementSelectors.size() - 1) { // Has more -> go deeper
String key = beanPropertyElementSelector.getPropertyName();
Map<String, Object> resultMapTmp = (Map<String, Object>) resultMap.get(key);
if (resultMapTmp == null) {
resultMapTmp = new LinkedHashMap<String, Object>();
resultMap.put(key, resultMapTmp);
resultMap = resultMapTmp;
}
}
}
idx++;
}
return resultMap;
}
private String getLastName(NodePath path) {
return ((BeanPropertyElementSelector) path.getLastElementSelector()).getPropertyName();
}
}