merge
Deep merges two DataObjects together, returning a new DataObject containing the merged data.
This method will merge data according to the following rules
DataObjects - will have all keys from
thisand other in the returned DataObjectDataLists - will always prefer the value in the other object if present. DataList contents are never merged.
The depth parameter controls how many levels of DataObject nesting should be merged. After the given depth, if a DataObject is found in both this and other objects, then the latter is chosen in its entirety.
Example
val lhs = DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-1", DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-2", DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-3", DataObject.create {
put("key1", "string")
put("key2", true)
})
})
})
}
val rhs = DataObject.create {
put("key1", "new string")
put("lvl-1", DataObject.create {
put("key1", "new string")
put("lvl-2", DataObject.create {
put("key1", "new string")
put("key3", 1)
put("lvl-3", DataObject.create {
put("key1", "new string")
})
})
})
}
val merged = lhs.merge(rhs, 2)
// merged will be the equivalent of this:
DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("lvl-1", DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("lvl-2", DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("key3", 1) // from rhs
put("lvl-3", DataObject.create {
put("key1", "new string") // from rhs only
})
})
})
}The default value for depth is Int.MAX_VALUE, and will deep merge all levels. Zero or negative values will be equivalent to the DataObject.plus operator.
Parameters
The incoming object, whose key/values are to merged into the current object.
Optional limit on the number of levels deep to merge.