Skip to content

Commit 4b99159

Browse files
committed
Change default JSON serializer to use jackson instead of GSON. It allows ordered JSON serialization of maps
1 parent d6338b9 commit 4b99159

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

pom.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@
5151

5252
<dependencies>
5353
<dependency>
54-
<groupId>com.google.code.gson</groupId>
55-
<artifactId>gson</artifactId>
56-
<version>2.8.5</version>
54+
<groupId>com.fasterxml.jackson.core</groupId>
55+
<artifactId>jackson-core</artifactId>
56+
<version>2.9.8</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.fasterxml.jackson.core</groupId>
60+
<artifactId>jackson-databind</artifactId>
61+
<version>2.9.8</version>
5762
</dependency>
5863
<dependency>
5964
<groupId>org.projectlombok</groupId>

src/main/java/io/github/jsonSnapshot/SnapshotMatcher.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.github.jsonSnapshot;
22

3-
import com.google.gson.GsonBuilder;
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.core.util.DefaultIndenter;
6+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
7+
import com.fasterxml.jackson.core.util.Separators;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.SerializationFeature;
410
import org.apache.commons.lang3.ArrayUtils;
511
import org.apache.commons.lang3.StringUtils;
612
import org.assertj.core.util.Arrays;
@@ -89,8 +95,45 @@ public static Snapshot expect(Object firstObject, Object... others) {
8995
}
9096
}
9197

92-
private static Function<Object, String> defaultJsonFunction() {
93-
return (object) -> new GsonBuilder().setPrettyPrinting().create().toJson(object);
98+
static Function<Object, String> defaultJsonFunction() {
99+
100+
ObjectMapper objectMapper = buildObjectMapper();
101+
102+
DefaultPrettyPrinter pp = buildDefaultPrettyPrinter();
103+
104+
return (object) -> {
105+
try {
106+
return objectMapper.writer(pp).writeValueAsString(object);
107+
} catch (Exception e) {
108+
throw new SnapshotMatchException(e.getMessage());
109+
}
110+
};
111+
}
112+
113+
private static DefaultPrettyPrinter buildDefaultPrettyPrinter() {
114+
DefaultPrettyPrinter pp = new DefaultPrettyPrinter(""){
115+
@Override
116+
public DefaultPrettyPrinter withSeparators(Separators separators) {
117+
this._separators = separators;
118+
this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " ";
119+
return this;
120+
}
121+
};
122+
pp.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
123+
return pp;
124+
}
125+
126+
private static ObjectMapper buildObjectMapper() {
127+
ObjectMapper objectMapper = new ObjectMapper();
128+
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
129+
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
130+
131+
objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
132+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
133+
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
134+
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
135+
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
136+
return objectMapper;
94137
}
95138

96139
private static void validateExpectCall(Snapshot snapshot) {

src/test/java/io/github/jsonSnapshot/SnapshotIntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ public void shouldThrowStackOverflowError() {
6565
fakeObject1.setFakeObject(fakeObject2);
6666
fakeObject2.setFakeObject(fakeObject1);
6767

68-
assertThrows(StackOverflowError.class, () -> expect(fakeObject1).toMatchSnapshot());
68+
assertThrows(SnapshotMatchException.class, () -> expect(fakeObject1).toMatchSnapshot());
6969
}
7070
}

src/test/java/io/github/jsonSnapshot/SnapshotTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.jsonSnapshot;
22

3-
import com.google.gson.GsonBuilder;
43
import org.junit.jupiter.api.AfterEach;
54
import org.junit.jupiter.api.BeforeEach;
65
import org.junit.jupiter.api.Test;
@@ -34,8 +33,7 @@ public class SnapshotTest {
3433
public void setUp() throws NoSuchMethodException, IOException {
3534
snapshotFile = new SnapshotFile(DEFAULT_CONFIG.getFilePath(), "anyFilePath");
3635
snapshot = new Snapshot(snapshotFile, String.class,
37-
String.class.getDeclaredMethod("toString"),
38-
(object) -> new GsonBuilder().setPrettyPrinting().create().toJson(object),"anyObject");
36+
String.class.getDeclaredMethod("toString"), SnapshotMatcher.defaultJsonFunction(),"anyObject");
3937
}
4038

4139
@AfterEach

0 commit comments

Comments
 (0)