private static void merge(ObjectNode tree,ObjectNode overrideTree){
  Iterator<String> fieldNames=overrideTree.fieldNames();
  while (fieldNames.hasNext()) {
    String fieldName=fieldNames.next();
    JsonNode primaryValue=tree.get(fieldName);
    JsonNode backupValue=overrideTree.get(fieldName);
    if (primaryValue == null) {
      if (backupValue != null) {
        tree.set(fieldName,backupValue);
      }
    }
 else     if (backupValue.isNull()) {
      tree.remove(fieldName);
    }
 else     if (primaryValue.isObject() && backupValue.isObject()) {
      merge((ObjectNode)primaryValue,(ObjectNode)backupValue.deepCopy());
    }
 else {
      tree.set(fieldName,overrideTree.get(fieldName));
    }
  }
}

private static void merge ( ObjectNode tree , ObjectNode overrideTree ) { Iterator < String > fieldNames = overrideTree . fieldNames ( ) ; while ( fieldNames . hasNext ( ) ) { String fieldName = fieldNames . next ( ) ; JsonNode primaryValue = tree . get ( fieldName ) ; JsonNode backupValue = overrideTree . get ( fieldName ) ; if ( primaryValue == null ) { if ( backupValue != null ) { tree . set ( fieldName , backupValue ) ; } } else if ( backupValue . isNull ( ) ) { tree . remove ( fieldName ) ; } else if ( primaryValue . isObject ( ) && backupValue . isObject ( ) ) { merge ( ( ObjectNode ) primaryValue , ( ObjectNode ) backupValue . deepCopy ( ) ) ; } else { tree . set ( fieldName , overrideTree . get ( fieldName ) ) ; } } }
overwrites the fields of tree that intersect with those of overridetree . < p > values of tree are modified , and values of overridetree are not modified . the merge process loops through the fields of tree and replaces non - object values with the corresponding value from overridetree if present . object values are traversed recursively to replace sub - properties . null fields in overridetree are removed from tree .
