Skip to content

Commit ee2d52e

Browse files
committed
Resolve #276: merge branch 'blackwinter-json-encoder-pretty-prefix'
2 parents 05e81f7 + de8e7b3 commit ee2d52e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

metafacture-json/src/main/java/org/metafacture/json/JsonEncoder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.fasterxml.jackson.core.SerializableString;
3535
import com.fasterxml.jackson.core.io.CharacterEscapes;
3636
import com.fasterxml.jackson.core.io.SerializedString;
37+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
3738

3839
/**
3940
* Serialises an object as JSON. Records and entities are represented
@@ -66,11 +67,7 @@ public JsonEncoder() {
6667
}
6768

6869
public void setPrettyPrinting(final boolean prettyPrinting) {
69-
if (prettyPrinting) {
70-
jsonGenerator.useDefaultPrettyPrinter();
71-
} else {
72-
jsonGenerator.setPrettyPrinter(null);
73-
}
70+
jsonGenerator.setPrettyPrinter(prettyPrinting ? new DefaultPrettyPrinter((SerializableString) null) : null);
7471
}
7572

7673
public boolean getPrettyPrinting() {

metafacture-json/src/test/java/org/metafacture/json/JsonEncoderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ public void testIssue152ShouldNotPrefixOutputWithSpaces() {
181181
ordered.verify(receiver).process(fixQuotes("{'L2':'V2'}"));
182182
}
183183

184+
@Test
185+
public void testShouldNotPrefixPrettyPrintedOutputWithSpaces() {
186+
encoder.setPrettyPrinting(true);
187+
188+
encoder.startRecord("");
189+
encoder.literal(LITERAL1, VALUE1);
190+
encoder.endRecord();
191+
encoder.startRecord("");
192+
encoder.literal(LITERAL2, VALUE2);
193+
encoder.endRecord();
194+
195+
final InOrder ordered = inOrder(receiver);
196+
ordered.verify(receiver).process(fixQuotes("{\n 'L1' : 'V1'\n}"));
197+
ordered.verify(receiver).process(fixQuotes("{\n 'L2' : 'V2'\n}"));
198+
}
199+
184200
/*
185201
* Utility method which replaces all single quotes in a string with double quotes.
186202
* This allows to specify the JSON output in the test cases without having to wrap

0 commit comments

Comments
 (0)