foodev.jsondiff
Class JsonPatch

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

public class JsonPatch
extends Object

Patch tool for differences as produced by JsonDiff.diff(String, String).

The patch is a JSON object where members order is not defined, however when applying the patch order matters when editing arrays. Therefore all deletions are done before additions, and more specifically for array deletions the order is reverse.

Syntax:

 
 {
   "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 added after 3 becoming the new 4 (current 4 pushed right)
   "~key[+4]":{ "sub": "array add"} // ERROR (nonsense)
   "-key[4]:  0                     // removing element 4 current 5 becoming new 4 (value is ignored)
   "-key[+4]: 0                     // ERROR (nonsense)
 }
 
 

Instruction order is merge, set, insert, delete. This is important when altering arrays, since insertions will affect the array index of subsequent delete instructions.

Author:
Martin Algesten

Constructor Summary
JsonPatch()
           
 
Method Summary
static void apply(Object orig, Object patch)
          Patches the first argument with the second.
static String apply(String orig, String patch)
          Modifies the given original JSON object using the instructions provided and returns the result.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonPatch

public JsonPatch()
Method Detail

apply

public static String apply(String orig,
                           String patch)
                    throws IllegalArgumentException
Modifies the given original JSON object using the instructions provided and returns the result. Each argument is expected to be a JSON object {}.

Parameters:
orig - The original JSON object to modify.
patch - The set of instructions to use.
Returns:
The modified JSON object.
Throws:
IllegalArgumentException - if the given arguments are not accepted.
JsonWrapperException - if the strings can't be parsed as JSON.

apply

public static void apply(Object orig,
                         Object patch)
Patches the first argument with the second. Accepts two GSON JsonObject or (if jar is provided) a Jackson style ObjectNode.

Parameters:
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).
Throws:
IllegalArgumentException - if the given arguments are not accepted.


Copyright © 2012. All Rights Reserved.