diff --git a/src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java b/src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java index 04bdf9f..13184a3 100644 --- a/src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java +++ b/src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.EnumSet; +import java.util.Iterator; class InPlaceApplyProcessor implements JsonPatchProcessor { @@ -142,9 +143,28 @@ else if (parentNode.isArray()) } private void addToObject(JsonPointer path, JsonNode node, JsonNode value) { - final ObjectNode target = (ObjectNode) node; String key = path.last().getField(); - target.set(key, value); + if (node.has(key) && node.get(key).isObject()) { + mergeInBase(node.get(key), value); + } + else { + final ObjectNode target = (ObjectNode) node; + target.set(key, value); + } + } + + public void mergeInBase (JsonNode base, JsonNode value) { + Iterator fieldsIterator = value.fieldNames(); + + while (fieldsIterator.hasNext()) { + String fieldName = fieldsIterator.next(); + if (base.has(fieldName) && base.get(fieldName).isObject()) { + mergeInBase(base.get(fieldName), value.get(fieldName)); + } else { + ObjectNode baseObjectNode = (ObjectNode) base; + baseObjectNode.set(fieldName, value.get(fieldName)); + } + } } private void addToArray(JsonPointer path, JsonNode value, JsonNode parentNode) {