JSONUtils { public static JSONObject diff (JSONObject prev, JSONObject curr){ JSONObject diff = new JSONObject(); for (String attr : curr.keySet()){ if (!prev.has(attr)){ diff.put(attr, curr.get(attr)); } if (prev.has(attr) && !prev.get(attr).equals(curr.get(attr))){ diff.put(attr, curr.get(attr)); } } return diff; }  static JSONObject diff(JSONObject prev, JSONObject curr); static JSONObject apply(JSONObject json, JSONObject patch); static JSONArray splitJsonArray(int chunkSize, JSONArray nodes); static void keyChange(Map<String, Object> map, String regex, String replace); static ArrayList toArrayList(Collection collection); static Map toMap(JSONObject input); static ArrayList toArray(JSONArray jsonArray); static JSONArray toJson(Set set); static JSONObject toJson(Map map); static void main(String[] args);  }
@Test public void testDiff() { JSONObject first = new JSONObject() .put("foo", "bar") .put("hodor", "hodor"); JSONObject second = new JSONObject() .put("foo", "bar") .put("hodor", "hodor!") .put("hold", "door"); JSONObject patch = JSONUtils.diff(first, second); JSONObject patchedVer = JSONUtils.apply(first, patch); assertEquals(patchedVer.toString(),"{\"foo\":\"bar\",\"hodor\":\"hodor!\",\"hold\":\"door\"}"); }