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
This commit is contained in:
parent
cdf8439c9b
commit
05ca686833
@ -29,11 +29,17 @@
|
|||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
<version>${jackson.version}</version>
|
<version>${jackson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>${guava.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<json.version>20211205</json.version>
|
<json.version>20211205</json.version>
|
||||||
<gson.version>2.10.1</gson.version>
|
<gson.version>2.10.1</gson.version>
|
||||||
|
<guava.version>32.1.2-jre</guava.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -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<String, Object> flatten(Map<String, Object> map) {
|
||||||
|
return flatten(map, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> flatten(Map<String, Object> map, String prefix) {
|
||||||
|
Map<String, Object> flatMap = new HashMap<>();
|
||||||
|
map.forEach((key, value) -> {
|
||||||
|
String newKey = prefix != null ? prefix + "." + key : key;
|
||||||
|
if (value instanceof Map) {
|
||||||
|
flatMap.putAll(flatten((Map<String, Object>) 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<Map<String, Object>> list = (List<Map<String, Object>>) value;
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
flatMap.putAll(flatten(list.get(i), newKey + "[" + i + "]"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
flatMap.put(newKey, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return flatMap;
|
||||||
|
}
|
||||||
|
}
|
@ -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<String, Object> jsonFileToMap(String path) throws IOException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
return mapper.readValue(new File(path), new TypeReference<Map<String, Object>>() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> jsonFileToMapGson(String path) throws IOException {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
return gson.fromJson(new FileReader(path), new TypeToken<Map<String, Object>>() {}.getType());
|
||||||
|
}
|
||||||
|
}
|
@ -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<String, Object> firstMap = JsonUtils.jsonFileToMap("src/test/resources/first.json");
|
||||||
|
Map<String, Object> secondMap = JsonUtils.jsonFileToMap("src/test/resources/second.json");
|
||||||
|
|
||||||
|
MapDifference<String, Object> 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<String, Object> firstFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/first.json"));
|
||||||
|
Map<String, Object> secondFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/second.json"));
|
||||||
|
|
||||||
|
MapDifference<String, Object> difference = Maps.difference(firstFlatMap, secondFlatMap);
|
||||||
|
difference.entriesDiffering().forEach((key, value) -> {
|
||||||
|
System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue());
|
||||||
|
});
|
||||||
|
assertThat(difference.areEqual()).isFalse();
|
||||||
|
}
|
||||||
|
}
|
22
json-modules/json-conversion/src/test/resources/first.json
Normal file
22
json-modules/json-conversion/src/test/resources/first.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
22
json-modules/json-conversion/src/test/resources/second.json
Normal file
22
json-modules/json-conversion/src/test/resources/second.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user