foodev.jsondiff
Class JsonDiff

java.lang.Object
  extended by foodev.jsondiff.JsonDiff

public class JsonDiff
extends Object

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:

Example: {a:[{b:1,c:2},{d:3}]}
 
Becomes a list of:
  1. Leaf: obj
  2. Leaf: array 0
  3. Leaf: obj
  4. Leaf: b: 1
  5. Leaf: c: 2
  6. Leaf: array 1
  7. Leaf: obj
  8. Leaf: d: 3

Author:
Martin Algesten

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

JsonDiff

public JsonDiff()
Method Detail

diff

public static String diff(String from,
                          String to)
                   throws IllegalArgumentException,
                          JsonWrapperException
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. Both from/to are expected to be objects {}.

Parameters:
from - The origin to transform
to - The desired result
Returns:
The set of instructions to go from -> to as a JSON object {}.
Throws:
IllegalArgumentException - if the given arguments are not accepted.
JsonWrapperException - if the strings can't be parsed as JSON.

diff

public static Object diff(Object from,
                          Object to)
                   throws IllegalArgumentException
Runs a diff using underlying JSON parser implementations. Accepts two GSON JsonObject or (if jar is provided) a Jackson style ObjectNode. The returned type is the same as the received.

Parameters:
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).
Returns:
Object containing the instructions. The type will be the same as that passed in constructor.
Throws:
IllegalArgumentException - if the given arguments are not accepted.


Copyright © 2012. All Rights Reserved.