How to use the jsonpatch.JsonPatch.from_string function in jsonpatch

To help you get started, we’ve selected a few jsonpatch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack / python-glanceclient / tests / v2 / test_schemas.py View on Github external
def compare_json_patches(a, b):
    """Return 0 if a and b describe the same JSON patch."""
    return JsonPatch.from_string(a) == JsonPatch.from_string(b)
github cernanalysispreservation / analysispreservation.cern.ch / invenio_data / modules / jsonedit / views.py View on Github external
def edit(path):
    db.session.begin(subtransactions=True)
    try:
        # the DB record won't change after this point
        record = get_record(path)
        if not record:
            abort(404)

        if request.method == 'POST':
            # generate patch
            data_old = dict(record)
            patch = None
            if request.form['patch']:
                # prioritize `patch` variant
                patch = jsonpatch.JsonPatch.from_string(request.form['patch'])
            elif request.form['json']:
                # hard override, generate a patch for that
                data_new_should = json.loads(request.form['json'])
                patch = jsonpatch.make_patch(data_old, data_new_should)

            if patch:
                # create new record
                try:
                    record = record.patch(patch)
                except Exception as e:
                    patch_str = patch.to_string()
                    return make_response(
                        render_template(
                            'jsonedit/conflict.html',
                            reason='Could not apply patch: "{}".'.format(str(e)),
                            patch=json.loads(patch_str),
github f5devcentral / f5-cccl / f5_cccl / resource / resource.py View on Github external
def _retrieve_whitelist_updates(self):
        """Retrieves the updates and ret to this whitelisted object"""

        updates = None
        if not self._whitelist:
            LOGGER.error('Cannot retrieve updates to the non-whitelisted '
                         'object %s', self.full_path())
        else:
            if self._whitelist_updates is not None:
                try:
                    update_str = zlib.decompress(base64.b64decode(
                        self._whitelist_updates)).decode('ascii')
                    updates = jsonpatch.JsonPatch.from_string(update_str)
                except Exception:  # pylint: disable=broad-except
                    LOGGER.error('Cannot process previous updates for the '
                                 'whitelisted resource %s', self.full_path())
        return updates
github core-api / python-client / coreapi / transports / websockets.py View on Github external
def _diff_content(headers, body, diff):
    patch = jsonpatch.JsonPatch.from_string(diff)
    previous_data = json.loads(body)
    next_data = jsonpatch.apply_patch(previous_data, patch)
    return json.dumps(next_data)
github canonical / cloud-init / cloudinit / handlers / cloud_config.py View on Github external
def _merge_patch(self, payload):
        # JSON doesn't handle comments in this manner, so ensure that
        # if we started with this 'type' that we remove it before
        # attempting to load it as json (which the jsonpatch library will
        # attempt to do).
        payload = payload.lstrip()
        payload = util.strip_prefix_suffix(payload, prefix=JSONP_PREFIX)
        patch = jsonpatch.JsonPatch.from_string(payload)
        LOG.debug("Merging by applying json patch %s", patch)
        self.cloud_buf = patch.apply(self.cloud_buf, in_place=False)