|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object foodev.jsondiff.JsonDiff
public class JsonDiff
Util for comparing two json-objects and create a new object with a set of instructions to transform the first to the
second. The output of this util can be fed into JsonPatch#apply(JzonObject, JzonObject)
.
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:
Becomes a list of:
Example: {a:[{b:1,c:2},{d:3}]}
Constructor Summary | |
---|---|
JsonDiff()
|
Method Summary | |
---|---|
static Object |
diff(Object from,
Object to)
Runs a diff using underlying JSON parser implementations. |
static 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JsonDiff()
Method Detail |
---|
public static String diff(String from, String to) throws IllegalArgumentException, JsonWrapperException
from
- The origin to transformto
- The desired result
IllegalArgumentException
- if the given arguments are not accepted.
JsonWrapperException
- if the strings can't be parsed as JSON.public static Object diff(Object from, Object to) throws IllegalArgumentException
JsonObject
or (if jar is
provided) a Jackson style ObjectNode
. The returned type is the same as the received.
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |