json_diff [--output FILE] [--unified | --normal]
[--strip NUM] [--reverse] [originalfile] [patchfile]
json_diff [--version]
json_diff [--help]
json_patch applies diffs in the format produced by json_diff(1) to JSON-serialized data structures.
The program attempts to mimic the interface of the patch(1) utility as far as possible, while also remaining compatible with the script functionality of the json_delta.py library on which it relies. There are, therefore, at least four different ways its input can be specified.
The most salient departure from the behavior of patch(1) is that, by default, json_patch will not modify files in place. Instead, the patched structure is written as JSON to stdout. Frankly, this is to save having to implement backup filename options, getting it wrong, and having angry hackers blame me for their lost data.
However, the input structure is read into memory before the output file handle is opened, so an in-place modification can be accomplished by setting the option --output to point to <originalfile>.
Also, note that json_diff and json_patch can only manipulate a single file at a time: even the output of json_diff -u is not a “unified” diff sensu stricto.
--output FILE, -o FILE | |
Write output to FILE instead of stdout. | |
--unified, -u | Force the patch to be interpreted as a udiff. |
--normal, -n | Force the patch to be interpreted as a normal (i.e. JSON-format) patch |
--reverse | Assume the patch was created with old and new files swapped. |
--string NUM | Strip NUM leading components from file names read out of udiff headers. |
--version | Show the program’s version number and exit. |
--help, -h | Show a brief help message and exit. |
The program has strict requirements of the format of “unified” diffs. It works by discarding header lines, then creating two strings: one by discarding every line beginning with -, then discarding the first character of every remaining line, and one following the same procedure, but with lines beginnig with + discarded. For json_patch to function, these strings must be interpretable according to a superset of the JSON spec, which I will now describe:
By interpreting the strings extracted from the udiff according to this format, json_patch arrives at a subset of each of the structures that it expresses the delta between. To construct its output, it then replaces the ... constructions with object properties and/or array elements from the structure to be patched (if they are not present in the other subset, which would indicate that the diff expects that that node in the structure is to be deleted).