@Test public void testSortJsonEntriesByKeys() throws Exception { JsonNode data = JsonHandler.getFromInputStream(PropertiesUtils.getResourceAsStream("json_sorter/input.json")); JsonSorter.sortJsonEntriesByKeys(data, Arrays.asList("field1", "field2")); JsonNode expected = JsonHandler.getFromInputStream(PropertiesUtils.getResourceAsStream("json_sorter/expected.json")); JsonAssert.assertJsonEquals(JsonHandler.unprettyPrint(data), JsonHandler.unprettyPrint(expected)); }
@VisibleForTesting public static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys) { if (jsonNode == null) return; for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) { JsonNode value = it.next(); sortJsonEntriesByKeys(value, orderedKeys); } if (jsonNode.isArray() && jsonNode.size() > 1 && jsonNode.get(0).isObject()) { ArrayNode arrayNode = (ArrayNode) jsonNode; List<ObjectNode> items = new ArrayList<>(); jsonNode.forEach(i -> items.add((ObjectNode) i)); items.sort((node1, node2) -> { for (String sortKey : orderedKeys) { JsonNode jsonValue1 = node1.get(sortKey); JsonNode jsonValue2 = node2.get(sortKey); if (jsonValue1 == null && jsonValue2 == null) { } else if (jsonValue1 == null) { return -1; } else if (jsonValue2 == null) { return 1; } else { if (jsonValue1.isNull() && jsonValue2.isNull()) { } else if (jsonValue1.isNull()) { return -1; } else if (jsonValue2.isNull()) { return 1; } else { int sort = jsonValue1.asText().compareTo(node2.get(sortKey).asText()); if (sort != 0) return sort; } } } return 1; }); arrayNode.removeAll(); arrayNode.addAll(items); } }
JsonSorter { @VisibleForTesting public static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys) { if (jsonNode == null) return; for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) { JsonNode value = it.next(); sortJsonEntriesByKeys(value, orderedKeys); } if (jsonNode.isArray() && jsonNode.size() > 1 && jsonNode.get(0).isObject()) { ArrayNode arrayNode = (ArrayNode) jsonNode; List<ObjectNode> items = new ArrayList<>(); jsonNode.forEach(i -> items.add((ObjectNode) i)); items.sort((node1, node2) -> { for (String sortKey : orderedKeys) { JsonNode jsonValue1 = node1.get(sortKey); JsonNode jsonValue2 = node2.get(sortKey); if (jsonValue1 == null && jsonValue2 == null) { } else if (jsonValue1 == null) { return -1; } else if (jsonValue2 == null) { return 1; } else { if (jsonValue1.isNull() && jsonValue2.isNull()) { } else if (jsonValue1.isNull()) { return -1; } else if (jsonValue2.isNull()) { return 1; } else { int sort = jsonValue1.asText().compareTo(node2.get(sortKey).asText()); if (sort != 0) return sort; } } } return 1; }); arrayNode.removeAll(); arrayNode.addAll(items); } } }
JsonSorter { @VisibleForTesting public static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys) { if (jsonNode == null) return; for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) { JsonNode value = it.next(); sortJsonEntriesByKeys(value, orderedKeys); } if (jsonNode.isArray() && jsonNode.size() > 1 && jsonNode.get(0).isObject()) { ArrayNode arrayNode = (ArrayNode) jsonNode; List<ObjectNode> items = new ArrayList<>(); jsonNode.forEach(i -> items.add((ObjectNode) i)); items.sort((node1, node2) -> { for (String sortKey : orderedKeys) { JsonNode jsonValue1 = node1.get(sortKey); JsonNode jsonValue2 = node2.get(sortKey); if (jsonValue1 == null && jsonValue2 == null) { } else if (jsonValue1 == null) { return -1; } else if (jsonValue2 == null) { return 1; } else { if (jsonValue1.isNull() && jsonValue2.isNull()) { } else if (jsonValue1.isNull()) { return -1; } else if (jsonValue2.isNull()) { return 1; } else { int sort = jsonValue1.asText().compareTo(node2.get(sortKey).asText()); if (sort != 0) return sort; } } } return 1; }); arrayNode.removeAll(); arrayNode.addAll(items); } } private  JsonSorter(); }
JsonSorter { @VisibleForTesting public static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys) { if (jsonNode == null) return; for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) { JsonNode value = it.next(); sortJsonEntriesByKeys(value, orderedKeys); } if (jsonNode.isArray() && jsonNode.size() > 1 && jsonNode.get(0).isObject()) { ArrayNode arrayNode = (ArrayNode) jsonNode; List<ObjectNode> items = new ArrayList<>(); jsonNode.forEach(i -> items.add((ObjectNode) i)); items.sort((node1, node2) -> { for (String sortKey : orderedKeys) { JsonNode jsonValue1 = node1.get(sortKey); JsonNode jsonValue2 = node2.get(sortKey); if (jsonValue1 == null && jsonValue2 == null) { } else if (jsonValue1 == null) { return -1; } else if (jsonValue2 == null) { return 1; } else { if (jsonValue1.isNull() && jsonValue2.isNull()) { } else if (jsonValue1.isNull()) { return -1; } else if (jsonValue2.isNull()) { return 1; } else { int sort = jsonValue1.asText().compareTo(node2.get(sortKey).asText()); if (sort != 0) return sort; } } } return 1; }); arrayNode.removeAll(); arrayNode.addAll(items); } } private  JsonSorter(); @VisibleForTesting static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys); }
JsonSorter { @VisibleForTesting public static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys) { if (jsonNode == null) return; for (Iterator<JsonNode> it = jsonNode.elements(); it.hasNext(); ) { JsonNode value = it.next(); sortJsonEntriesByKeys(value, orderedKeys); } if (jsonNode.isArray() && jsonNode.size() > 1 && jsonNode.get(0).isObject()) { ArrayNode arrayNode = (ArrayNode) jsonNode; List<ObjectNode> items = new ArrayList<>(); jsonNode.forEach(i -> items.add((ObjectNode) i)); items.sort((node1, node2) -> { for (String sortKey : orderedKeys) { JsonNode jsonValue1 = node1.get(sortKey); JsonNode jsonValue2 = node2.get(sortKey); if (jsonValue1 == null && jsonValue2 == null) { } else if (jsonValue1 == null) { return -1; } else if (jsonValue2 == null) { return 1; } else { if (jsonValue1.isNull() && jsonValue2.isNull()) { } else if (jsonValue1.isNull()) { return -1; } else if (jsonValue2.isNull()) { return 1; } else { int sort = jsonValue1.asText().compareTo(node2.get(sortKey).asText()); if (sort != 0) return sort; } } } return 1; }); arrayNode.removeAll(); arrayNode.addAll(items); } } private  JsonSorter(); @VisibleForTesting static void sortJsonEntriesByKeys(JsonNode jsonNode, List<String> orderedKeys);  }