public class JSONDelta
extends java.lang.Object
The purpose of this class is to produce a "DELTA" comparison of two JSON files.
{
"title": "title",
"pgNumTot": 84,
"weight": 1.05,
"dist": [
{"id":"01", "country":"Italy"},
{"id":"02", "country":"Brazil"},
{"id":"08", "country":"Canada"},
{"id":"09", "country":"Mexico"}
],
"missing": "missing"
}
{
"title": "title",
"pgNumTot": 84,
"weight": 2.22,
"wordCount": 16000,
"dist": [
{"id":"01", "country":"Italy"},
{"id":"02", "country":"Mexico"},
{"id":"05", "country":"France"},
{"id":"08", "country":"Canada"}
],
"z-extra": "superfluous"
}
Basically, if an object member is the same between the two, then that member is omitted. If a child object is exactly the same, it will be omitted. Only the members that are changed will be included in the delta. If a new member is present then it is included. If a member value goes away, then it is represented as a null string (""). If you have a list of simple values, and the list changes, then the entire new list is included in the delta.
There is special handling for lists of objects. The list must have a key field identified, and the objects in the list are compared by key. If the object (and all children of that object) are the same, then it is omitted from the output. If a new object appears with a new key value, then that new object is included so that it can be created along with all children. To delete an object from a list, a special value is used that you can set, but it is @delete by default, and this causes the entire object to be deleted on the receiving side. The output would then be as follows: the title and page num tot remained the same, so they will be omitted. The weight changed, so the new weight will be included. For the list, there must be a key field, and according to the key if the object with the same key exists, and it is unchanged, then that object is omitted. If there is a new object with a new key the entire object will be included. If there is an existing object
{
"weight": 2.22,
"wordCount": 16000,
"dist": [
{"id":"09", "@op":"@delete"},
{"id":"02", "country":"Mexico"},
{"id":"05", "country":"France"},
],
"missing": "",
"z-extra": "superfluous"
}
| Constructor and Description |
|---|
JSONDelta() |
| Modifier and Type | Method and Description |
|---|---|
JSONObject |
createDelta(JSONObject oldObj,
JSONObject newObj)
Creates a JSONObject that represents the delta of the two JSON objects
passed in.
|
static void |
main(java.lang.String[] args)
The main routine can be called as a command-line command
where you pass the names of JSON files.
|
void |
setDeletedValueIndicator(java.lang.String newValue) |
void |
setListKeyMap(java.util.HashMap<java.lang.String,java.lang.String> newValue) |
void |
setObjectDeleteIndicator(java.lang.String newKey,
java.lang.String newValue) |
public void setDeletedValueIndicator(java.lang.String newValue)
public void setObjectDeleteIndicator(java.lang.String newKey,
java.lang.String newValue)
public void setListKeyMap(java.util.HashMap<java.lang.String,java.lang.String> newValue)
public JSONObject createDelta(JSONObject oldObj, JSONObject newObj) throws java.lang.Exception
Creates a JSONObject that represents the delta of the two JSON objects passed in. Each member that is the same is omitted, and only the members that are new or changed in the second parameter are returned.
java.lang.Exceptionpublic static void main(java.lang.String[] args)
The main routine can be called as a command-line command where you pass the names of JSON files. The files are read, and the result is written out as a CSV file.
JSONDelta {First-File.json} {Second-File.json} [-a]
First parameter and second parameter are the two files to read as JSON files and to compare. If either file is not a valid JSON syntax you will get an error. There is a third, optional parameter (-a) which controls the reportAll setting.
The output will be written to second file name with "delta.json" on the end. In the example above, the file would be written to Second-File.JSONDelta.csv