How to use the jsonpatch.apply_patch 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 stefankoegl / python-json-patch / tests.py View on Github external
def test_arrays_one_element_sequences(self):
        """ Tests the case of multiple common one element sequences inside an array """
        # see https://github.com/stefankoegl/python-json-patch/issues/30#issuecomment-155070128
        src = [1,2,3]
        dst = [3,1,4,2]
        patch = jsonpatch.make_patch(src, dst)
        res = jsonpatch.apply_patch(src, patch)
        self.assertEqual(res, dst)
github stefankoegl / python-json-patch / tests.py View on Github external
def test_move_object_key(self):
        obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
               'qux': {'corge': 'grault'}}
        res = jsonpatch.apply_patch(obj, [{'op': 'move', 'from': '/foo/waldo',
                                           'path': '/qux/thud'}])
        self.assertEqual(res, {'qux': {'thud': 'fred', 'corge': 'grault'},
                               'foo': {'bar': 'baz'}})
github stefankoegl / python-json-patch / tests.py View on Github external
def test_add_nested(self):
        # see http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-03#appendix-A.10
        src = {"foo": "bar"}
        patch_obj = [ { "op": "add", "path": "/child", "value": { "grandchild": { } } } ]
        res = jsonpatch.apply_patch(src, patch_obj)
        expected = { "foo": "bar",
                      "child": { "grandchild": { } }
                   }
        self.assertEqual(expected, res)
github stefankoegl / python-json-patch / tests.py View on Github external
def test_issue103(self):
        """In JSON 1 is different from 1.0 even though in python 1 == 1.0"""
        src = {'A': 1}
        dst = {'A': 1.0}
        patch = jsonpatch.make_patch(src, dst)
        res = jsonpatch.apply_patch(src, patch)
        self.assertEqual(res, dst)
        self.assertIsInstance(res['A'], float)
github stefankoegl / python-json-patch / tests.py View on Github external
def test_add_array_item(self):
        obj = {'foo': ['bar', 'baz']}
        res = jsonpatch.apply_patch(obj, [{'op': 'add', 'path': '/foo/1', 'value': 'qux'}])
        self.assertEqual(res['foo'], ['bar', 'qux', 'baz'])
github biothings / myvariant.info / src / utils / diff.py View on Github external
def apply_patch(doc, patch):
    return jsonpatch.apply_patch(doc, patch)
github RCOSDP / weko / modules / invenio-records / invenio_records / api.py View on Github external
def patch(self, patch):
        """Patch record metadata.

        :params patch: Dictionary of record metadata.
        :returns: A new :class:`Record` instance.
        """
        data = apply_patch(dict(self), patch)
        return self.__class__(data, model=self.model)
github gnocchixyz / gnocchi / gnocchi / rest / __init__.py View on Github external
try:
            rt = pecan.request.indexer.get_resource_type(self._name)
        except indexer.NoSuchResourceType as e:
            abort(404, e)
        enforce("update resource type", rt)

        # Ensure this is a valid jsonpatch dict
        patch = deserialize_and_validate(
            ResourceTypeJsonPatchSchema,
            expected_content_types=["application/json-patch+json"])

        # Add new attributes to the resource type
        rt_json_current = rt.jsonify()
        try:
            rt_json_next = jsonpatch.apply_patch(rt_json_current, patch)
        except jsonpatch.JsonPatchException as e:
            abort(400, e)
        del rt_json_next['state']

        # Validate that the whole new resource_type is valid
        schema = pecan.request.indexer.get_resource_type_schema()
        try:
            rt_json_next = voluptuous.Schema(schema.for_update, required=True)(
                rt_json_next)
        except voluptuous.Error as e:
            abort(400, "Invalid input: %s" % e)

        # Get only newly formatted and deleted attributes
        add_attrs = {k: v for k, v in rt_json_next["attributes"].items()
                     if k not in rt_json_current["attributes"]}
        del_attrs = [k for k in rt_json_current["attributes"]