Skip to content

Commit 11407b4

Browse files
authoredDec 14, 2023
#patch: Fixes #162 - Stable formatting between Jackson 2.16 and earlier versions
Unfortunatily this reaches even deeper into Jackson internals than before, so it might break in the future (again). On the other hand, this sidesteps messing with reflection or other hacks to decide on which version of Jackson we are currently running.
1 parent e9521ab commit 11407b4

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed
 

Diff for: ‎java-snapshot-testing-plugin-jackson/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ dependencies {
2121
testImplementation 'org.assertj:assertj-core:3.11.1'
2222
testImplementation 'org.skyscreamer:jsonassert:1.5.0' // For docs/ reporter example
2323

24-
testImplementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
25-
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3'
26-
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.3'
27-
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3'
24+
testImplementation 'com.fasterxml.jackson.core:jackson-core:2.16.0'
25+
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'
26+
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.0'
27+
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.0'
2828
}
2929

3030
test { useJUnitPlatform() }

Diff for: ‎java-snapshot-testing-plugin-jackson/src/main/java/au/com/origin/snapshots/jackson/serializers/v1/JacksonSnapshotSerializer.java

+1-25
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,14 @@
88
import com.fasterxml.jackson.annotation.JsonAutoDetect;
99
import com.fasterxml.jackson.annotation.JsonInclude;
1010
import com.fasterxml.jackson.core.PrettyPrinter;
11-
import com.fasterxml.jackson.core.util.DefaultIndenter;
12-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
13-
import com.fasterxml.jackson.core.util.Separators;
1411
import com.fasterxml.jackson.databind.ObjectMapper;
1512
import com.fasterxml.jackson.databind.SerializationFeature;
1613
import java.util.Arrays;
1714
import java.util.List;
1815

1916
public class JacksonSnapshotSerializer implements SnapshotSerializer {
2017

21-
private final PrettyPrinter pp =
22-
new DefaultPrettyPrinter("") {
23-
{
24-
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
25-
this.indentArraysWith(lfOnlyIndenter);
26-
this.indentObjectsWith(lfOnlyIndenter);
27-
}
28-
29-
// It's a requirement
30-
// @see https://github.com/FasterXML/jackson-databind/issues/2203
31-
public DefaultPrettyPrinter createInstance() {
32-
return new DefaultPrettyPrinter(this);
33-
}
34-
35-
@Override
36-
public DefaultPrettyPrinter withSeparators(Separators separators) {
37-
this._separators = separators;
38-
this._objectFieldValueSeparatorWithSpaces =
39-
separators.getObjectFieldValueSeparator() + " ";
40-
return this;
41-
}
42-
};
18+
private final PrettyPrinter pp = new SnapshotPrettyPrinter();
4319
private final ObjectMapper objectMapper =
4420
new ObjectMapper() {
4521
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package au.com.origin.snapshots.jackson.serializers.v1;
2+
3+
import com.fasterxml.jackson.core.util.DefaultIndenter;
4+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
5+
6+
class SnapshotPrettyPrinter extends DefaultPrettyPrinter {
7+
8+
public SnapshotPrettyPrinter() {
9+
super("");
10+
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
11+
this.indentArraysWith(lfOnlyIndenter);
12+
this.indentObjectsWith(lfOnlyIndenter);
13+
14+
this._objectFieldValueSeparatorWithSpaces =
15+
this._separators.getObjectFieldValueSeparator() + " ";
16+
}
17+
18+
// It's a requirement
19+
// @see https://github.com/FasterXML/jackson-databind/issues/2203
20+
public DefaultPrettyPrinter createInstance() {
21+
return new DefaultPrettyPrinter(this);
22+
}
23+
}

0 commit comments

Comments
 (0)