Breadcrumbs

JsonDiff

JsonDiff is helpful if you have two versions of an object, and want to know if some attributes have changed, and if so which ones. For simple object types with 5-10 attributes Action Script might suffice, but if you have complex, nested structures, it's not fun. That's where JsonDiff can help. Just convert both versions to their JSON representation using classToJson(), and let JsonDiff find the differences. The output is a JSON array of changes that need to applied to get the latter version from the former (see below).

What is a RFC 6902 JSON Patch?

If we had two JSON documents

JSON
{"a": 0,"b": [1,2]}
JSON
{"b": [1,2,0]}

the patch to obtain b from a was

[{"op":"move","from":"/a","path":"/b/2"}]
jsondiff-example-calc-diff-input.PNG
jsondiff-example-calc-diff-output.PNG

Operation Signatures

JsonDiff offers the following base operations:

  • calcDiff

  • calcDiffExcept

  • applyDiff

  • isEmptyDiff
    (a convenience method for testing a calculated diff for being "empty", meaning the two JSONs are equal)

jsondiff-class-jsondiff.PNG

Synopsis

Suppose you have an xUML class A, and two instances a1 and a2 with JSON representations js1=a1.classToJson() and js2=a2.classToJson(). Then the following is true:

  • applyDiff(js1, calcDiff(js1, js2)) == js2

    • calcDiff(js1, js2) => js12diff (see example section below)

    • applyDiff(js1, js12diff) => jsx, with jsx == js2

  • isEmptyDiff(calcDiff(js1, js1)) == true

  • calcDiffExcept(js1, js2, ignoredAttributes[]) behaves like calcDiff, but will not report difference in attributes that are explicitly ignored (the e2e-type attribute sometimes generated by classToJson() is ignored automatically).

  • the "normalized" parameter steers whether ADD/REMOVE pairs of the same object are normalized to MOVE (usually recommended with structures containing arrays)

📗

Related Pages:

📘

Related Documentation: