@Test public void filterJsonNodeWithNoMatchingFields() 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\"}}"); List fieldsToBeEncrypted = Arrays.asList(); JsonNode outputNode = JSONUtils.filterJsonNodeWithFields(originalNode, fieldsToBeEncrypted); assertEquals(null, outputNode); }
public static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields) { if(checkIfNoFieldExistsInJsonNode(jsonNode, filterFields)) return null; ObjectMapper mapper = new ObjectMapper(new JsonFactory()); if(jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; ObjectNode filteredObjectNode = mapper.createObjectNode(); Iterator<String> fieldIterator = objectNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); if(filterFields.contains(field) && !objectNode.get(field).isNull()) { filteredObjectNode.set(field, objectNode.get(field)); } else { JsonNode filteredJsonNode = filterJsonNodeWithFields(objectNode.get(field), filterFields); if(filteredJsonNode != null) { filteredObjectNode.set(field, filteredJsonNode); } } } if(filteredObjectNode.isEmpty(mapper.getSerializerProvider())) return null; return filteredObjectNode; } else if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; ArrayNode filteredArrayNode = mapper.createArrayNode(); for(int i = 0; i < arrayNode.size(); i++) { JsonNode filteredJsonNode = filterJsonNodeWithFields(arrayNode.get(i), filterFields); if(filteredJsonNode == null) { if(arrayNode.get(i).isArray()) filteredJsonNode = mapper.createArrayNode(); else if(arrayNode.get(i).isObject()) filteredJsonNode = mapper.createObjectNode(); else filteredJsonNode = NullNode.getInstance(); } filteredArrayNode.add(filteredJsonNode); } return filteredArrayNode; } return null; }
JSONUtils { public static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields) { if(checkIfNoFieldExistsInJsonNode(jsonNode, filterFields)) return null; ObjectMapper mapper = new ObjectMapper(new JsonFactory()); if(jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; ObjectNode filteredObjectNode = mapper.createObjectNode(); Iterator<String> fieldIterator = objectNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); if(filterFields.contains(field) && !objectNode.get(field).isNull()) { filteredObjectNode.set(field, objectNode.get(field)); } else { JsonNode filteredJsonNode = filterJsonNodeWithFields(objectNode.get(field), filterFields); if(filteredJsonNode != null) { filteredObjectNode.set(field, filteredJsonNode); } } } if(filteredObjectNode.isEmpty(mapper.getSerializerProvider())) return null; return filteredObjectNode; } else if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; ArrayNode filteredArrayNode = mapper.createArrayNode(); for(int i = 0; i < arrayNode.size(); i++) { JsonNode filteredJsonNode = filterJsonNodeWithFields(arrayNode.get(i), filterFields); if(filteredJsonNode == null) { if(arrayNode.get(i).isArray()) filteredJsonNode = mapper.createArrayNode(); else if(arrayNode.get(i).isObject()) filteredJsonNode = mapper.createObjectNode(); else filteredJsonNode = NullNode.getInstance(); } filteredArrayNode.add(filteredJsonNode); } return filteredArrayNode; } return null; } }
JSONUtils { public static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields) { if(checkIfNoFieldExistsInJsonNode(jsonNode, filterFields)) return null; ObjectMapper mapper = new ObjectMapper(new JsonFactory()); if(jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; ObjectNode filteredObjectNode = mapper.createObjectNode(); Iterator<String> fieldIterator = objectNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); if(filterFields.contains(field) && !objectNode.get(field).isNull()) { filteredObjectNode.set(field, objectNode.get(field)); } else { JsonNode filteredJsonNode = filterJsonNodeWithFields(objectNode.get(field), filterFields); if(filteredJsonNode != null) { filteredObjectNode.set(field, filteredJsonNode); } } } if(filteredObjectNode.isEmpty(mapper.getSerializerProvider())) return null; return filteredObjectNode; } else if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; ArrayNode filteredArrayNode = mapper.createArrayNode(); for(int i = 0; i < arrayNode.size(); i++) { JsonNode filteredJsonNode = filterJsonNodeWithFields(arrayNode.get(i), filterFields); if(filteredJsonNode == null) { if(arrayNode.get(i).isArray()) filteredJsonNode = mapper.createArrayNode(); else if(arrayNode.get(i).isObject()) filteredJsonNode = mapper.createObjectNode(); else filteredJsonNode = NullNode.getInstance(); } filteredArrayNode.add(filteredJsonNode); } return filteredArrayNode; } return null; }  }
JSONUtils { public static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields) { if(checkIfNoFieldExistsInJsonNode(jsonNode, filterFields)) return null; ObjectMapper mapper = new ObjectMapper(new JsonFactory()); if(jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; ObjectNode filteredObjectNode = mapper.createObjectNode(); Iterator<String> fieldIterator = objectNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); if(filterFields.contains(field) && !objectNode.get(field).isNull()) { filteredObjectNode.set(field, objectNode.get(field)); } else { JsonNode filteredJsonNode = filterJsonNodeWithFields(objectNode.get(field), filterFields); if(filteredJsonNode != null) { filteredObjectNode.set(field, filteredJsonNode); } } } if(filteredObjectNode.isEmpty(mapper.getSerializerProvider())) return null; return filteredObjectNode; } else if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; ArrayNode filteredArrayNode = mapper.createArrayNode(); for(int i = 0; i < arrayNode.size(); i++) { JsonNode filteredJsonNode = filterJsonNodeWithFields(arrayNode.get(i), filterFields); if(filteredJsonNode == null) { if(arrayNode.get(i).isArray()) filteredJsonNode = mapper.createArrayNode(); else if(arrayNode.get(i).isObject()) filteredJsonNode = mapper.createObjectNode(); else filteredJsonNode = NullNode.getInstance(); } filteredArrayNode.add(filteredJsonNode); } return filteredArrayNode; } return null; }  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 filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields) { if(checkIfNoFieldExistsInJsonNode(jsonNode, filterFields)) return null; ObjectMapper mapper = new ObjectMapper(new JsonFactory()); if(jsonNode.isObject()) { ObjectNode objectNode = (ObjectNode) jsonNode; ObjectNode filteredObjectNode = mapper.createObjectNode(); Iterator<String> fieldIterator = objectNode.fieldNames(); while (fieldIterator.hasNext()) { String field = fieldIterator.next(); if(filterFields.contains(field) && !objectNode.get(field).isNull()) { filteredObjectNode.set(field, objectNode.get(field)); } else { JsonNode filteredJsonNode = filterJsonNodeWithFields(objectNode.get(field), filterFields); if(filteredJsonNode != null) { filteredObjectNode.set(field, filteredJsonNode); } } } if(filteredObjectNode.isEmpty(mapper.getSerializerProvider())) return null; return filteredObjectNode; } else if(jsonNode.isArray()) { ArrayNode arrayNode = (ArrayNode) jsonNode; ArrayNode filteredArrayNode = mapper.createArrayNode(); for(int i = 0; i < arrayNode.size(); i++) { JsonNode filteredJsonNode = filterJsonNodeWithFields(arrayNode.get(i), filterFields); if(filteredJsonNode == null) { if(arrayNode.get(i).isArray()) filteredJsonNode = mapper.createArrayNode(); else if(arrayNode.get(i).isObject()) filteredJsonNode = mapper.createObjectNode(); else filteredJsonNode = NullNode.getInstance(); } filteredArrayNode.add(filteredJsonNode); } return filteredArrayNode; } return null; }  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);  }