-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/danielbechler/diff/node/DiffJSONVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/de/danielbechler/diff/node/DiffJSONVisitorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.danielbechler.diff.node | ||
|
||
import de.danielbechler.diff.ObjectDifferBuilder | ||
import spock.lang.Specification | ||
|
||
|
||
class DiffJSONVisitorTest extends Specification { | ||
|
||
DiffJSONVisitor visitor | ||
|
||
Something workingObject | ||
Something baseObject | ||
|
||
def setup() { | ||
workingObject = new Something(name: 'working name', someOther: new Something(name: 'working other'), theSame: 'theSame') | ||
baseObject = new Something(name: 'base name', someOther: new Something(name: 'base other'), theSame: 'theSame') | ||
visitor = new DiffJSONVisitor(workingObject, baseObject) | ||
} | ||
|
||
def "something to map"() { | ||
when: | ||
DiffNode root = ObjectDifferBuilder.buildDefault().compare(workingObject, baseObject) | ||
root.visit(visitor) | ||
Map<String, Object> messages = visitor.messagesAsMap | ||
|
||
then: | ||
messages.name == 'working name' | ||
messages.someOther.name == 'working other' | ||
} | ||
|
||
def "something to json"() { | ||
when: | ||
DiffNode root = ObjectDifferBuilder.buildDefault().compare(workingObject, baseObject) | ||
root.visit(visitor) | ||
String json = visitor.getAsJSON() | ||
|
||
then: | ||
json == '{"name":"working name","someOther":{"name":"working other"}}' | ||
} | ||
|
||
def "clearing a field"() { | ||
when: | ||
workingObject.name = null | ||
DiffNode root = ObjectDifferBuilder.buildDefault().compare(workingObject, baseObject) | ||
root.visit(visitor) | ||
String json = visitor.getAsJSON() | ||
|
||
then: | ||
json == '{"name":null,"someOther":{"name":"working other"}}' | ||
} | ||
|
||
static class Something { | ||
String name | ||
String theSame | ||
Something someOther | ||
} | ||
} |