@Test public void superimposeEncryptedDataOnOriginalNodeTest() throws IOException { JsonNode originalNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":{\"userName\":\"ajay\",\"name\":\"ajay\",\"gender\":\"male\"," + "\"mobileNumber\":\"12312312\",\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}}"); JsonNode encryptedNode = mapper.readTree("{\"User\":{\"userName\":\"123|jkafsdhkjhalfsj\",\"name\":\"123|" + "jkafsdhkjhalfsj\",\"mobileNumber\":\"123|hdskjahkjfk\"}}"); JsonNode outputNode = JSONUtils.merge(encryptedNode, originalNode); JsonNode expectedNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":{\"userName\":\"123|jkafsdhkjhalfsj\"," + "\"name\":\"123|jkafsdhkjhalfsj\",\"gender\":\"male\",\"mobileNumber\":\"123|hdskjahkjfk\"," + "\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}}"); assertEquals(expectedNode, outputNode); }
public static JsonNode merge(JsonNode newNode, JsonNode originalNode) { Iterator<String> fieldNames = originalNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = newNode.get(fieldName); if (jsonNode != null) { if (jsonNode.isObject()) { merge(jsonNode, originalNode.get(fieldName)); } else if (jsonNode.isArray()) { for (int i = 0; i < jsonNode.size(); i++) { merge(jsonNode.get(i), originalNode.get(fieldName).get(i)); } } } else { if (newNode instanceof ObjectNode) { JsonNode value = originalNode.get(fieldName); ((ObjectNode) newNode).set(fieldName, value); } } } return newNode; }
JSONUtils { public static JsonNode merge(JsonNode newNode, JsonNode originalNode) { Iterator<String> fieldNames = originalNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = newNode.get(fieldName); if (jsonNode != null) { if (jsonNode.isObject()) { merge(jsonNode, originalNode.get(fieldName)); } else if (jsonNode.isArray()) { for (int i = 0; i < jsonNode.size(); i++) { merge(jsonNode.get(i), originalNode.get(fieldName).get(i)); } } } else { if (newNode instanceof ObjectNode) { JsonNode value = originalNode.get(fieldName); ((ObjectNode) newNode).set(fieldName, value); } } } return newNode; } }
JSONUtils { public static JsonNode merge(JsonNode newNode, JsonNode originalNode) { Iterator<String> fieldNames = originalNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = newNode.get(fieldName); if (jsonNode != null) { if (jsonNode.isObject()) { merge(jsonNode, originalNode.get(fieldName)); } else if (jsonNode.isArray()) { for (int i = 0; i < jsonNode.size(); i++) { merge(jsonNode.get(i), originalNode.get(fieldName).get(i)); } } } else { if (newNode instanceof ObjectNode) { JsonNode value = originalNode.get(fieldName); ((ObjectNode) newNode).set(fieldName, value); } } } return newNode; }  }
JSONUtils { public static JsonNode merge(JsonNode newNode, JsonNode originalNode) { Iterator<String> fieldNames = originalNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = newNode.get(fieldName); if (jsonNode != null) { if (jsonNode.isObject()) { merge(jsonNode, originalNode.get(fieldName)); } else if (jsonNode.isArray()) { for (int i = 0; i < jsonNode.size(); i++) { merge(jsonNode.get(i), originalNode.get(fieldName).get(i)); } } } else { if (newNode instanceof ObjectNode) { JsonNode value = originalNode.get(fieldName); ((ObjectNode) newNode).set(fieldName, value); } } } return newNode; }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields); static JsonNode filterJsonNodeWithPaths2(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath); static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths); }
JSONUtils { public static JsonNode merge(JsonNode newNode, JsonNode originalNode) { Iterator<String> fieldNames = originalNode.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next(); JsonNode jsonNode = newNode.get(fieldName); if (jsonNode != null) { if (jsonNode.isObject()) { merge(jsonNode, originalNode.get(fieldName)); } else if (jsonNode.isArray()) { for (int i = 0; i < jsonNode.size(); i++) { merge(jsonNode.get(i), originalNode.get(fieldName).get(i)); } } } else { if (newNode instanceof ObjectNode) { JsonNode value = originalNode.get(fieldName); ((ObjectNode) newNode).set(fieldName, value); } } } return newNode; }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields); static JsonNode filterJsonNodeWithPaths2(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath); static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths);  }