Skip to content

Commit

Permalink
#patch: Fixes #162 - Stable formatting between Jackson 2.16 and earli…
Browse files Browse the repository at this point in the history
…er 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.
  • Loading branch information
TobiX authored Dec 14, 2023
1 parent e9521ab commit 11407b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
8 changes: 4 additions & 4 deletions java-snapshot-testing-plugin-jackson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ dependencies {
testImplementation 'org.assertj:assertj-core:3.11.1'
testImplementation 'org.skyscreamer:jsonassert:1.5.0' // For docs/ reporter example

testImplementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3'
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.3'
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3'
testImplementation 'com.fasterxml.jackson.core:jackson-core:2.16.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.0'
testRuntimeOnly 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.0'
}

test { useJUnitPlatform() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,14 @@
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.Separators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.Arrays;
import java.util.List;

public class JacksonSnapshotSerializer implements SnapshotSerializer {

private final PrettyPrinter pp =
new DefaultPrettyPrinter("") {
{
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
this.indentArraysWith(lfOnlyIndenter);
this.indentObjectsWith(lfOnlyIndenter);
}

// It's a requirement
// @see https://github.com/FasterXML/jackson-databind/issues/2203
public DefaultPrettyPrinter createInstance() {
return new DefaultPrettyPrinter(this);
}

@Override
public DefaultPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces =
separators.getObjectFieldValueSeparator() + " ";
return this;
}
};
private final PrettyPrinter pp = new SnapshotPrettyPrinter();
private final ObjectMapper objectMapper =
new ObjectMapper() {
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package au.com.origin.snapshots.jackson.serializers.v1;

import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;

class SnapshotPrettyPrinter extends DefaultPrettyPrinter {

public SnapshotPrettyPrinter() {
super("");
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
this.indentArraysWith(lfOnlyIndenter);
this.indentObjectsWith(lfOnlyIndenter);

this._objectFieldValueSeparatorWithSpaces =
this._separators.getObjectFieldValueSeparator() + " ";
}

// It's a requirement
// @see https://github.com/FasterXML/jackson-databind/issues/2203
public DefaultPrettyPrinter createInstance() {
return new DefaultPrettyPrinter(this);
}
}

0 comments on commit 11407b4

Please sign in to comment.