@Test public void TestDiff() { HashMap<String, Object> map1 = new HashMap<>(); HashMap<String, Object> map2 = new HashMap<>(); map1.put("bbbb", "cccc"); map1.put("xxx", "aaa"); map2.put("xxx", "aa"); map2.put("cccc", "bbbb"); map1.put("dict", ImmutableMap.builder().put("a", 1).put("b", 2) .put("em", ImmutableMap.builder().put("c", 3).build()).build()); map2.put("dict", ImmutableMap.builder().put("a", 1).put("b", 3) .put("em", ImmutableMap.builder().put("c", 4).put("d", 5).build()).build()); MapDifference diff = Maps.difference(map1, map2); Map diffMap = new HashMap(); JsonCompareUtil.getDetailsDiff(map1, map2, diffMap, ""); Assert.assertTrue(diffMap.containsKey("bbbb")); Assert.assertTrue(diffMap.containsKey("xxx")); Assert.assertTrue(diffMap.containsKey("cccc")); Assert.assertTrue(diffMap.containsKey("dict.b")); Assert.assertTrue(diffMap.containsKey("dict.em.c")); Assert.assertTrue(diffMap.containsKey("dict.em.d")); Assert.assertEquals(6, diffMap.size()); }
public static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs, String prefix) { MapDifference difference = Maps.difference(left, right); for (Object object : difference.entriesOnlyOnLeft().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{entry.getValue(), null}); } for (Object object : difference.entriesOnlyOnRight().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, entry.getValue()}); } for (Object object : difference.entriesDiffering().entrySet()) { Map.Entry entry = (Map.Entry) object; MapDifference.ValueDifference valueDiff = (MapDifference.ValueDifference) entry.getValue(); if (valueDiff.leftValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, valueDiff.rightValue()}); } else if (valueDiff.rightValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), null}); } else if (valueDiff.leftValue() instanceof Map) { String newPrefix = StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(); getDetailsDiff((Map) valueDiff.leftValue(), (Map) valueDiff.rightValue(), diffs, newPrefix); } else if (valueDiff.leftValue().getClass().isArray()) { if (!Arrays.equals((Object[]) valueDiff.leftValue(), (Object[]) valueDiff.rightValue())) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } else { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } }
JsonCompareUtil { public static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs, String prefix) { MapDifference difference = Maps.difference(left, right); for (Object object : difference.entriesOnlyOnLeft().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{entry.getValue(), null}); } for (Object object : difference.entriesOnlyOnRight().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, entry.getValue()}); } for (Object object : difference.entriesDiffering().entrySet()) { Map.Entry entry = (Map.Entry) object; MapDifference.ValueDifference valueDiff = (MapDifference.ValueDifference) entry.getValue(); if (valueDiff.leftValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, valueDiff.rightValue()}); } else if (valueDiff.rightValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), null}); } else if (valueDiff.leftValue() instanceof Map) { String newPrefix = StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(); getDetailsDiff((Map) valueDiff.leftValue(), (Map) valueDiff.rightValue(), diffs, newPrefix); } else if (valueDiff.leftValue().getClass().isArray()) { if (!Arrays.equals((Object[]) valueDiff.leftValue(), (Object[]) valueDiff.rightValue())) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } else { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } } }
JsonCompareUtil { public static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs, String prefix) { MapDifference difference = Maps.difference(left, right); for (Object object : difference.entriesOnlyOnLeft().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{entry.getValue(), null}); } for (Object object : difference.entriesOnlyOnRight().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, entry.getValue()}); } for (Object object : difference.entriesDiffering().entrySet()) { Map.Entry entry = (Map.Entry) object; MapDifference.ValueDifference valueDiff = (MapDifference.ValueDifference) entry.getValue(); if (valueDiff.leftValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, valueDiff.rightValue()}); } else if (valueDiff.rightValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), null}); } else if (valueDiff.leftValue() instanceof Map) { String newPrefix = StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(); getDetailsDiff((Map) valueDiff.leftValue(), (Map) valueDiff.rightValue(), diffs, newPrefix); } else if (valueDiff.leftValue().getClass().isArray()) { if (!Arrays.equals((Object[]) valueDiff.leftValue(), (Object[]) valueDiff.rightValue())) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } else { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } }  }
JsonCompareUtil { public static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs, String prefix) { MapDifference difference = Maps.difference(left, right); for (Object object : difference.entriesOnlyOnLeft().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{entry.getValue(), null}); } for (Object object : difference.entriesOnlyOnRight().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, entry.getValue()}); } for (Object object : difference.entriesDiffering().entrySet()) { Map.Entry entry = (Map.Entry) object; MapDifference.ValueDifference valueDiff = (MapDifference.ValueDifference) entry.getValue(); if (valueDiff.leftValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, valueDiff.rightValue()}); } else if (valueDiff.rightValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), null}); } else if (valueDiff.leftValue() instanceof Map) { String newPrefix = StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(); getDetailsDiff((Map) valueDiff.leftValue(), (Map) valueDiff.rightValue(), diffs, newPrefix); } else if (valueDiff.leftValue().getClass().isArray()) { if (!Arrays.equals((Object[]) valueDiff.leftValue(), (Object[]) valueDiff.rightValue())) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } else { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } }  static Map<String, Object[]> findDiff(E inst1, E inst2); static Map<String, Object[]> findDiff(E inst1, E inst2, Set<String> excludedFields); static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs,
                                    String prefix); }
JsonCompareUtil { public static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs, String prefix) { MapDifference difference = Maps.difference(left, right); for (Object object : difference.entriesOnlyOnLeft().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{entry.getValue(), null}); } for (Object object : difference.entriesOnlyOnRight().entrySet()) { Map.Entry entry = (Map.Entry) object; diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, entry.getValue()}); } for (Object object : difference.entriesDiffering().entrySet()) { Map.Entry entry = (Map.Entry) object; MapDifference.ValueDifference valueDiff = (MapDifference.ValueDifference) entry.getValue(); if (valueDiff.leftValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{null, valueDiff.rightValue()}); } else if (valueDiff.rightValue() == null) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), null}); } else if (valueDiff.leftValue() instanceof Map) { String newPrefix = StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(); getDetailsDiff((Map) valueDiff.leftValue(), (Map) valueDiff.rightValue(), diffs, newPrefix); } else if (valueDiff.leftValue().getClass().isArray()) { if (!Arrays.equals((Object[]) valueDiff.leftValue(), (Object[]) valueDiff.rightValue())) { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } else { diffs.put(StringUtils.isEmpty(prefix) ? entry.getKey().toString() : prefix + "." + entry.getKey().toString(), new Object[]{valueDiff.leftValue(), valueDiff.rightValue()}); } } }  static Map<String, Object[]> findDiff(E inst1, E inst2); static Map<String, Object[]> findDiff(E inst1, E inst2, Set<String> excludedFields); static void getDetailsDiff(Map left, Map right, Map<String, Object[]> diffs,
                                    String prefix); static final Logger logger; static final ObjectMapper DumpMapper; }