public class JsonDiff extends Object
Syntax for instructions:
{
"key": "replaced", // added or replacing key
"~key": "replaced", // added or replacing key (~ doesn't matter for primitive data types)
"key": null, // added or replacing key with null.
"~key": null, // added or replacing key with null (~ doesn't matter for null)
"-key": 0 // key removed (value is ignored)
"key": { "sub": "replaced" } // whole object "key" replaced
"~key": { "sub": "merged" } // key "sub" merged into object "key", rest of object untouched
"key": [ "replaced" ] // whole array added/replaced
"~key": [ "replaced" ] // whole array added/replaced (~ doesn't matter for whole array)
"key[4]": { "sub": "replaced" } // object replacing element 4, rest of array untouched
"~key[4]": { "sub": "merged"} // merging object at element 4, rest of array untouched
"key[+4]": { "sub": "array add"} // object inserted after 3 becoming the new 4 (current 4 pushed right)
"~key[+4]":{ "sub": "array add"} // object inserted after 3 becoming the new 4 (current 4 pushed right)
"-key[4]: 0 // removing element 4 current 5 becoming new 4 (value is ignored)
}
Instruction order is merge, set, insert, delete. This is important when altering arrays, since insertions will affect the array index of subsequent delete instructions.
When diffing, the object is expanded to a structure like this: Example: {a:[{b:1,c:2},{d:3}]}
Becomes a list of:
| Modifier and Type | Method and Description |
|---|---|
void |
apply(Object orig,
Object patch)
Patches the first argument with the second.
|
String |
apply(String orig,
String patch)
Modifies the given original JSON object using the instructions provided and returns the result.
|
Object |
diff(Object from,
Object to)
Runs a diff using underlying JSON parser implementations.
|
String |
diff(String from,
String to)
Runs a diff on the two given JSON objects given as string to produce another JSON object with instructions of how to transform the first argument to the second.
|
Visitor<?> |
getVisitor() |
void |
setVisitor(Visitor<?> visitor)
Registers a new visitor.
|
protected final Wrapper factory
public void apply(Object orig, Object patch)
orig - Object to patch. One of JsonObject or ObjectNode (if jar available).patch - Object holding patch instructions. One of JsonObject or ObjectNode (if jar available).IllegalArgumentException - if the given arguments are not accepted.public String apply(String orig, String patch) throws IllegalArgumentException
orig - The original JSON object to modify.patch - The set of instructions to use.IllegalArgumentException - if the given arguments are not accepted.public Object diff(Object from, Object to) throws IllegalArgumentException
from - Object to transform from. One of JsonObject or ObjectNode (if jar available).to - Object to transform to. One of JsonObject or ObjectNode (if jar available).IllegalArgumentException - if the given arguments are not accepted.public String diff(String from, String to) throws IllegalArgumentException
from - The origin to transformto - The desired resultIllegalArgumentException - if the given arguments are not accepted.Copyright © 2015. All Rights Reserved.