From 05ca68683341bc25cb4622b8f70cd73fbd024b83 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 30 Aug 2023 21:50:22 +0530 Subject: [PATCH] BAEL-6692 - converting json to map and comparing (#14630) * BAEL-6692 - converting json to map and comparing * BAEL-6692 - converting json to map and comparing * BAEL-6692 - converting json to map and comparing --- json-modules/json-conversion/pom.xml | 6 +++ .../com/baeldung/jsontomap/FlattenUtils.java | 36 ++++++++++++++++++ .../com/baeldung/jsontomap/JsonUtils.java | 23 ++++++++++++ .../jsontomap/JSONComparisonUnitTest.java | 37 +++++++++++++++++++ .../src/test/resources/first.json | 22 +++++++++++ .../src/test/resources/second.json | 22 +++++++++++ 6 files changed, 146 insertions(+) create mode 100644 json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/FlattenUtils.java create mode 100644 json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/JsonUtils.java create mode 100644 json-modules/json-conversion/src/test/java/com/baeldung/jsontomap/JSONComparisonUnitTest.java create mode 100644 json-modules/json-conversion/src/test/resources/first.json create mode 100644 json-modules/json-conversion/src/test/resources/second.json diff --git a/json-modules/json-conversion/pom.xml b/json-modules/json-conversion/pom.xml index 365c6d2ac9..680f27fa38 100644 --- a/json-modules/json-conversion/pom.xml +++ b/json-modules/json-conversion/pom.xml @@ -29,11 +29,17 @@ jackson-databind ${jackson.version} + + com.google.guava + guava + ${guava.version} + 20211205 2.10.1 + 32.1.2-jre diff --git a/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/FlattenUtils.java b/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/FlattenUtils.java new file mode 100644 index 0000000000..2fd49d99ad --- /dev/null +++ b/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/FlattenUtils.java @@ -0,0 +1,36 @@ +package com.baeldung.jsontomap; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class FlattenUtils { + public static Map flatten(Map map) { + return flatten(map, null); + } + + private static Map flatten(Map map, String prefix) { + Map flatMap = new HashMap<>(); + map.forEach((key, value) -> { + String newKey = prefix != null ? prefix + "." + key : key; + if (value instanceof Map) { + flatMap.putAll(flatten((Map) value, newKey)); + } else if (value instanceof List) { + // check for list of primitives + Object element = ((List) value).get(0); + if (element instanceof String || element instanceof Number || element instanceof Boolean) { + flatMap.put(newKey, value); + } else { + // check for list of objects + List> list = (List>) value; + for (int i = 0; i < list.size(); i++) { + flatMap.putAll(flatten(list.get(i), newKey + "[" + i + "]")); + } + } + } else { + flatMap.put(newKey, value); + } + }); + return flatMap; + } +} \ No newline at end of file diff --git a/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/JsonUtils.java b/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/JsonUtils.java new file mode 100644 index 0000000000..35339494fb --- /dev/null +++ b/json-modules/json-conversion/src/main/java/com/baeldung/jsontomap/JsonUtils.java @@ -0,0 +1,23 @@ +package com.baeldung.jsontomap; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Map; + +public class JsonUtils { + public static Map jsonFileToMap(String path) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readValue(new File(path), new TypeReference>() {}); + } + + public static Map jsonFileToMapGson(String path) throws IOException { + Gson gson = new Gson(); + return gson.fromJson(new FileReader(path), new TypeToken>() {}.getType()); + } +} diff --git a/json-modules/json-conversion/src/test/java/com/baeldung/jsontomap/JSONComparisonUnitTest.java b/json-modules/json-conversion/src/test/java/com/baeldung/jsontomap/JSONComparisonUnitTest.java new file mode 100644 index 0000000000..908161c258 --- /dev/null +++ b/json-modules/json-conversion/src/test/java/com/baeldung/jsontomap/JSONComparisonUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.jsontomap; + +import com.google.common.collect.MapDifference; +import com.google.common.collect.Maps; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JSONComparisonUnitTest { + + @Test + void givenTwoJsonFiles_whenCompared_thenTheyAreDifferent() throws IOException { + Map firstMap = JsonUtils.jsonFileToMap("src/test/resources/first.json"); + Map secondMap = JsonUtils.jsonFileToMap("src/test/resources/second.json"); + + MapDifference difference = Maps.difference(firstMap, secondMap); + difference.entriesDiffering().forEach((key, value) -> { + System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue()); + }); + assertThat(difference.areEqual()).isFalse(); + } + + @Test + void givenTwoJsonFiles_whenFlattenedAndCompared_thenTheyAreDifferent() throws IOException { + Map firstFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/first.json")); + Map secondFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/second.json")); + + MapDifference difference = Maps.difference(firstFlatMap, secondFlatMap); + difference.entriesDiffering().forEach((key, value) -> { + System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue()); + }); + assertThat(difference.areEqual()).isFalse(); + } +} diff --git a/json-modules/json-conversion/src/test/resources/first.json b/json-modules/json-conversion/src/test/resources/first.json new file mode 100644 index 0000000000..905859c59f --- /dev/null +++ b/json-modules/json-conversion/src/test/resources/first.json @@ -0,0 +1,22 @@ +{ + "name": "John", + "age": 30, + "cars": [ + "Ford", + "BMW" + ], + "address": { + "street": "Second Street", + "city": "New York" + }, + "children": [ + { + "name": "Sara", + "age": 5 + }, + { + "name": "Alex", + "age": 3 + } + ] +} \ No newline at end of file diff --git a/json-modules/json-conversion/src/test/resources/second.json b/json-modules/json-conversion/src/test/resources/second.json new file mode 100644 index 0000000000..503228519f --- /dev/null +++ b/json-modules/json-conversion/src/test/resources/second.json @@ -0,0 +1,22 @@ +{ + "name": "John", + "age": 30, + "cars": [ + "Ford", + "Audi" + ], + "address": { + "street": "Main Street", + "city": "New York" + }, + "children": [ + { + "name": "Peter", + "age": 5 + }, + { + "name": "Cathy", + "age": 10 + } + ] +} \ No newline at end of file