Python jsonmerge.merge() Examples
The following are 30
code examples of jsonmerge.merge().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
jsonmerge
, or try the search function
.
Example #1
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_custom_strategy(self): schema = {'mergeStrategy': 'myStrategy'} class MyStrategy(jsonmerge.strategies.Strategy): def merge(self, walk, base, head, schema, meta, **kwargs): if base is None: ref = "" else: ref = base.ref return JSONValue("foo", ref) merger = jsonmerge.Merger(schema=schema, strategies={'myStrategy': MyStrategy()}) base = None base = merger.merge(base, {'a': 1}) self.assertEqual(base, "foo")
Example #2
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_merge_by_id_non_unique_base(self): schema = { "mergeStrategy": "arrayMergeById", } base = [ {'id': 'a'}, {'id': 'a'} ] head = [ {'id': 'a', 'foo': 1} ] merger = jsonmerge.Merger(schema) with self.assertRaises(BaseInstanceError) as cm: merger.merge(base, head) self.assertEqual(cm.exception.value.ref, '#/1')
Example #3
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_merge_by_id_only_integers(self): # arrayMergeById strategy can be used to treat simple arrays of # integers as Python sets by setting idRef to root (i.e. pointing to # the array element itself) # # https://github.com/avian2/jsonmerge/issues/24 schema = { "mergeStrategy": "arrayMergeById", "mergeOptions": {"idRef": "/"}, } base = [ 1, 2 ] head = [ 2, 3 ] expected = [ 1, 2, 3] merger = jsonmerge.Merger(schema) base = merger.merge(base, head) self.assertEqual(base, expected)
Example #4
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_append_with_unique(self): schema = { "mergeStrategy": "append", "uniqueItems": True, } merger = jsonmerge.Merger(schema) head = ["a"] base = None base = merger.merge(base, head) base = merger.merge(base, head) schema2 = merger.get_schema() jsonschema.validate(head, schema2) jsonschema.validate(base, schema2)
Example #5
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_merge_by_id_no_key(self): schema = { "mergeStrategy": "arrayMergeById", } a = [ {"id": "A", "field": 1}, ] b = [ {'field': 2} ] merger = jsonmerge.Merger(schema) base = None base = merger.merge(base, a) base = merger.merge(base, b) # it should ignore array elements that do not have the id self.assertEqual(base, a)
Example #6
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_version_meta(self): schema = {'mergeStrategy': 'version'} merger = jsonmerge.Merger(schema) base = None base = merger.merge(base, "a", merge_options={ 'version': {'metadata': {'uri': 'http://example.com/a'}}}) base = merger.merge(base, "b", merge_options={ 'version': {'metadata': {'uri': 'http://example.com/b'}}}) self.assertEqual(base, [ {'value': "a", 'uri': 'http://example.com/a'}, {'value': "b", 'uri': 'http://example.com/b'}])
Example #7
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_discard_null_keep(self): schema = { 'properties': { 'a': { 'mergeStrategy': 'discard', 'mergeOptions': { 'keepIfUndef': True } } } } base = {} head = {'a': 1} base = jsonmerge.merge(base, head, schema) self.assertEqual(base, {'a': 1}) head = {'a': 2} base = jsonmerge.merge(base, head, schema) self.assertEqual(base, {'a': 1})
Example #8
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_anyof_overwrite_toplevel(self): schema = { 'mergeStrategy': 'overwrite', 'anyOf': [ { 'type': 'array' }, { 'type': 'string' }, ] } merger = jsonmerge.Merger(schema) self.assertEqual(merger.merge([2, 3, 4], 'a'), 'a') self.assertEqual(merger.merge('a', [2, 3, 4]), [2, 3, 4])
Example #9
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_oneof_overwrite_toplevel(self): schema = { 'mergeStrategy': 'overwrite', 'oneOf': [ { 'type': 'array' }, { 'type': 'string' }, ] } merger = jsonmerge.Merger(schema) self.assertEqual(merger.merge([2, 3, 4], 'a'), 'a') self.assertEqual(merger.merge('a', [2, 3, 4]), [2, 3, 4])
Example #10
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_merge_by_index_name_in_exception(self): schema = { 'properties': { 'a': { 'mergeStrategy': 'arrayMergeByIndex' } } } head = {'a': {}} base = {'a': []} merger = jsonmerge.Merger(schema) with self.assertRaises(HeadInstanceError) as cm: merger.merge(base, head) self.assertIn('arrayMergeByIndex', str(cm.exception))
Example #11
Source File: test_jsonmerge.py From jsonmerge with MIT License | 6 votes |
def test_internal_refs(self): schema = { 'id': 'http://example.com/schema_1.json', 'properties': { 'a': {'$ref': "#/definitions/a"}, }, 'definitions': { "a": { "properties": { "b": {'mergeStrategy': 'version'}, } }, } } merger = jsonmerge.Merger(schema) base = None base = merger.merge(base, {"a": {"b": "c"}}) base = merger.merge(base, {"a": {"b": "d"}}) self.assertEqual(base, {"a": {"b": [{"value": "c"}, {"value": "d"}]}})
Example #12
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_index(self): schema = { 'mergeStrategy': 'arrayMergeByIndex' } base = [ {'a': 0 }, {'b': 1} ] head = [ {'c': 2 }, {'d': 3} ] result = jsonmerge.merge(base, head, schema) self.assertEqual(result, [ {'a': 0, 'c': 2}, {'b': 1, 'd': 3} ])
Example #13
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_oneof_recursive(self): # Schema to merge all arrays with "append" strategy and all objects # with the default "objectMerge" strategy. schema = { "oneOf": [ { "type": "array", "mergeStrategy": "append" }, { "type": "object", "additionalProperties": { "$ref": "#" } }, { "type": "string" }, ] } base = {"a": ["1"], "b": "3", "c": {"d": ["4"], "e": "f"}} head = {"a": ["2"], "b": "4", "g": "7", "c": {"d": ["3"]}} merger = jsonmerge.Merger(schema) base = merger.merge(base, head) self.assertEqual(base, {"a": ["1", "2"], "b": "4", "g": "7", "c": {"d": ["4", "3"], "e": "f"}})
Example #14
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_nan(self): # float('nan') == float('nan') evaluates to false. # # https://github.com/avian2/jsonmerge/issues/39 base = { "foo": 1, "bar": float('nan') } head = { "foo": 1, "bar": float('nan') } base = jsonmerge.merge(base, head)
Example #15
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_oneof_recursive(self): # Schema to merge all arrays with "append" strategy and all objects # with the default "objectMerge" strategy. schema = { "oneOf": [ { "type": "array", "mergeStrategy": "append" }, { "type": "object", "additionalProperties": { "$ref": "#" } }, { "type": "string" }, ] } merger = jsonmerge.Merger(schema) schema2 = merger.get_schema() self.assertEqual(schema2, schema)
Example #16
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_str_with_name(self): e = SchemaError("Test error", JSONValue({}, '#'), 'test') self.assertEqual(str(e), "'test' merge strategy: Test error: #")
Example #17
Source File: mkcfg.py From RackHD with Apache License 2.0 | 5 votes |
def add_from_dict(self, add_dict): """ merge a dictionary into config dict :param add_dict: :return: """ self.__merge(add_dict)
Example #18
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_bad_strategy(self): schema = { 'properties': { 'a': { 'mergeStrategy': 'invalidStrategy' } } } base = {'a': 1 } head = {'a': 2 } with self.assertRaises(SchemaError) as cm: jsonmerge.merge(base, head, schema) self.assertEqual(cm.exception.value.ref, '#/properties/a')
Example #19
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_order_issue_31_2(self): schema = { "mergeStrategy": "arrayMergeById", } base = [ {'id': 'a', 'val': {'a': 1}}, {'id': 'b', 'val': {'b': 2}}, ] head = [ # this caused "Unresolvable JSON pointer" {'id': 'c', 'val': {'c': 3}}, {'id': 'a', 'val': {'c': 3}} ] expected = [ {'id': 'a', 'val': {'a': 1, 'c': 3}}, {'id': 'b', 'val': {'b': 2}}, {'id': 'c', 'val': {'c': 3}} ] merger = jsonmerge.Merger(schema) base = merger.merge(base, head) self.assertEqual(base, expected)
Example #20
Source File: mkcfg.py From RackHD with Apache License 2.0 | 5 votes |
def __merge(self, json_blob): """ json merge a json blob into config_dict :param json_blob: :return: """ schema = {'mergeStrategy': 'objectMerge'} self.config_dict = jsonmerge.merge(self.config_dict, json_blob, schema)
Example #21
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_discard_arraymergebyid_null(self): schema = { 'mergeStrategy': 'arrayMergeById', 'items': { 'mergeStrategy': 'discard' } } base = [ ] head = [ {'id': 1, 'val': 1} ] base = jsonmerge.merge(base, head, schema) self.assertEqual(base, [])
Example #22
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_bad_base_type(self): schema = { 'mergeStrategy': 'arrayMergeById' } head = [] base = {'foo': 'bar'} merger = jsonmerge.Merger(schema) with self.assertRaises(BaseInstanceError) as cm: merger.merge(base, head) self.assertEqual(cm.exception.value.ref, '#')
Example #23
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_bad_head_type(self): schema = { 'mergeStrategy': 'arrayMergeById' } head = {'foo': 'bar'} base = [] merger = jsonmerge.Merger(schema) with self.assertRaises(HeadInstanceError) as cm: merger.merge(base, head) self.assertEqual(cm.exception.value.ref, '#')
Example #24
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_complex_id(self): schema = { "mergeStrategy": "arrayMergeById", } a = [ {"id": ["A", {"B": "C"} ], "field": 1}, {"id": ["A", {"B": "D"} ], "field": 2}, {"id": ["A", {"B": "E"} ], "field": 3}, ] b = [ {"id": ["A", {"B": "D"} ], "field": 4}, {"id": ["E", {"B": "C"} ], "field": 5}, ] merger = jsonmerge.Merger(schema) c = merger.merge(a, b) expected = [ {"id": ["A", {"B": "C"} ], "field": 1}, {"id": ["A", {"B": "D"} ], "field": 4}, {"id": ["A", {"B": "E"} ], "field": 3}, {"id": ["E", {"B": "C"} ], "field": 5}, ] self.assertEqual(expected, c)
Example #25
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_compex_ref(self): schema = { "mergeStrategy": "arrayMergeById", "mergeOptions": {"idRef": "/foo/bar"}, } a = [ {'foo': {'bar': 1}, 'baz': 1} ] b = [ {'foo': {'bar': 2}} ] c = [ {'foo': {'bar': 1}, 'baz': 2} ] # by default, it should fall back to "replace" strategy for integers. expected = [ {'foo': {'bar': 1}, 'baz': 2}, {'foo': {'bar': 2}} ] merger = jsonmerge.Merger(schema) base = None base = merger.merge(base, a) base = merger.merge(base, b) base = merger.merge(base, c) self.assertEqual(base, expected)
Example #26
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_merge_by_id_no_items(self): schema = { "mergeStrategy": "arrayMergeById", "mergeOptions": {"idRef": "id"}, } a = [ {"id": "A", "field": 1}, ] b = [ {"id": "A", "field": 2}, ] # by default, it should fall back to "replace" strategy for integers. expected = [ {"id": "A", "field": 2}, ] merger = jsonmerge.Merger(schema) base = None base = merger.merge(base, a) base = merger.merge(base, b) self.assertEqual(base, expected)
Example #27
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_oneof_multiple_validate(self): schema = { 'oneOf': [ { 'type': 'array', 'maxItems': 3, 'mergeStrategy': 'append' }, { 'type': 'array', 'minItems': 2, 'mergeStrategy': 'overwrite' } ] } merger = jsonmerge.Merger(schema) base = [1] base = merger.merge(base, [2]) self.assertEqual(base, [1, 2]) base = [1, 2] with self.assertRaises(HeadInstanceError) as cm: base = merger.merge(base, [3, 4])
Example #28
Source File: import24.py From community-edition-setup with MIT License | 5 votes |
def restoreConfig(ldifFolder, newLdif, ldifModFolder): logging.info('Comparing old LDAP data and creating `modify` files.') ignoreList = ['objectClass', 'ou', 'oxAuthJwks', 'oxAuthConfWebKeys'] current_config_dns = getDns(newLdif) oldDnMap = getOldEntryMap(ldifFolder) for dn in oldDnMap.keys(): old_entry = getEntry("%s/%s" % (ldifFolder, oldDnMap[dn]), dn) if dn not in current_config_dns: addEntry(dn, old_entry, ldifModFolder) continue new_entry = getEntry(newLdif, dn) for attr in old_entry.keys(): # Note: Prefixing with part length helps in inserting base # before leaf entries in the LDAP # Filename = DN_part_length + unique random string new_fn = str(len(dn.split(','))) + '_' + str(uuid.uuid4()) filename = '%s/%s.ldif' % (ldifModFolder, new_fn) if attr in ignoreList: continue if attr not in new_entry: writeMod(dn, attr, old_entry[attr], filename, True) logging.debug("Adding attr %s to %s", attr, dn) elif old_entry[attr] != new_entry[attr]: mod_list = None if len(old_entry[attr]) == 1: try: logging.debug("Merging json value for %s", attr) old_json = json.loads(old_entry[attr][0]) new_json = json.loads(new_entry[attr][0]) new_json = merge(new_json, old_json) mod_list = [json.dumps(new_json)] except: mod_list = old_entry[attr] logging.debug("Keeping old value for %s", attr) else: mod_list = old_entry[attr] logging.debug("Keeping multiple old value for %s", attr) writeMod(dn, attr, mod_list, filename)
Example #29
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_oneof(self): schema = { 'oneOf': [ { 'type': 'array', 'mergeStrategy': 'append' }, { 'type': 'object' } ] } merger = jsonmerge.Merger(schema) base = [1] base = merger.merge(base, [2]) self.assertEqual(base, [1,2]) base = {'a': 1} base = merger.merge(base, {'b': 2}) self.assertEqual(base, {'a': 1, 'b': 2}) base = [1] with self.assertRaises(HeadInstanceError) as cm: merger.merge(base, {'b': 2}) self.assertEqual(cm.exception.value.ref, '#')
Example #30
Source File: test_jsonmerge.py From jsonmerge with MIT License | 5 votes |
def test_external_refs_draft6(self): schema_1 = { '$id': 'http://example.com/schema_1.json', 'properties': { 'a': {'$ref': "schema_2.json#/definitions/a"}, }, } schema_2 = { '$id': 'http://example.com/schema_2.json', 'definitions': { "a": { "properties": { "b": {'mergeStrategy': 'version'}, } }, } } merger = jsonmerge.Merger(schema_1, validatorclass=Draft6Validator) # merge() would otherwise make a HTTP request merger.cache_schema(schema_2) base = None base = merger.merge(base, {"a": {"b": "c"}}) base = merger.merge(base, {"a": {"b": "d"}}) self.assertEqual(base, {"a": {"b": [{"value": "c"}, {"value": "d"}]}})