Add a function for performing a JSON Merge Patch.

Review Request #9262 — Created Oct. 11, 2017 and submitted

Christian Hammond

This introduces djblets.util.json_utils.json_merge_patch, an
implementation of JSON Merge Patch (RFC 7396), which is used to apply a
set of changes to a JSON-compatible data structure. Merge Patches allow
for adding new values (which may be complex JSON objects) to
dictionaries or arrays, remove keys from dictionaries or arrays, or
replace existing values.

This has additional support for access control around keys, preventing
certain keys from being replaced or added, optionally aborting the
patching process if encountered.

Unit tests pass.

Tested manually with several targets and dictionaries.

  • 0
  • 0
  • 2
  • 0
  • 2
Description From Last Updated
Christian Hammond
David Trowbridge
  2. djblets/util/ (Diff revision 2)

    It seems to me that if something fails halfway through we'll end up with a halfway applied patch. Do we want to change it to operate on a copy of the target and then replace at the very end?

    1. It copies each level of the dictionary that's being modified. It doesn't modify the original dictionary.

    2. Going to flesh out the unit tests to check that the original dictionary is untouched.

Christian Hammond
Review request changed

Change Summary:

  • Renamed the "target" attribute (and related variables/docs) to "doc", helping make it clear that the target itself isn't being modified.
  • Updated unit tests to sanity-check that no operations end up modifying the original document.




Revision 3 (+366)

Show changes

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.


David Trowbridge
  1. Ship It!
Christian Hammond
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-0.10.x (defeb39)