-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDiffGenerator.java
More file actions
41 lines (33 loc) · 1.3 KB
/
DiffGenerator.java
File metadata and controls
41 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.deblock.jsondiff;
import com.deblock.jsondiff.diff.JsonDiff;
import com.deblock.jsondiff.matcher.JsonMatcher;
import com.deblock.jsondiff.matcher.Path;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.stream.Collectors;
public class DiffGenerator {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static JsonDiff diff(String expected, String actual, JsonMatcher jsonMatcher) {
return jsonMatcher.diff(Path.ROOT, read(expected), read(actual));
}
public static List<JsonDiff> diff(String expected, List<String> actualValues, JsonMatcher jsonMatcher) {
final var expectedObject = read(expected);
return actualValues.stream()
.map(actual -> jsonMatcher.diff(Path.ROOT, expectedObject, read(actual)))
.collect(Collectors.toList());
}
private static JsonNode read(String json) {
try {
return OBJECT_MAPPER.readTree(json);
} catch (JacksonException e) {
throw new JsonReadException(e);
}
}
public static class JsonReadException extends RuntimeException {
public JsonReadException(Throwable e) {
super(e);
}
}
}