This is the JsonDiff common library. |
Main workhorse for JSON Comparator
Parameters: |
|
---|
Example:
>>> from opslib.icsutils.jsondiff import Comparator
>>> import json
>>> old_json = {
... "name": "opslib",
... "version": "1.2.0",
... "members": {
... "role": "ops",
... "group": [ "ops", "devops" ]
... }
... }
>>> new_json = {
... "name": "opslib",
... "version": "1.3.0",
... "members": {
... "role": "devops",
... "group": [ "devops" ]
... }
... }
>>> json.dump(old_json, open("old.json", "w"))
>>> json.dump(new_json, open("new.json", "w"))
>>> fp_old = open("old.json", "r")
>>> fp_new = open("new.json", "r")
>>> engine = Comparator(fp_old, fp_new)
>>> res = engine.compare_dicts()
>>> print json.dumps(res, sort_keys=True, indent=4)
{
"members": {
"group": {
"0": {
"+++": "devops",
"---": "ops"
},
"1": {
"---": "devops"
}
},
"role": {
"+++": "devops",
"---": "ops"
}
},
"version": {
"+++": "1.3.0",
"---": "1.2.0"
}
}
list of weak references to the object (if defined)
simpler version of compare_dicts; just an internal method, because it could never be called from outside.
We have it guaranteed that both new_arr and old_arr are of type list.
Be careful with the result of this function. Negative answer from this function is really None, not False, so deciding based on the return value like in
if self._compare_scalars(...):
leads to wrong answer (it should be if self._compare_scalars(...) is not None:)
Whole -i or -x functionality. Rather than complicate logic while going through the object’s tree we filter the result of plain comparison.
Also clear out unused keys in result