Skip to content

Commit 5867d2c

Browse files
committed
Issue #190 - avoid writing newline before first object/array
1. Added a protected inNone() method to JsonGeneratorImpl so that subclasses can determine when we are in the outermost scope. I'm not attached to the name. 2. Added a check to the if statement in JsonPrettyGeneratorImpl.writeComma() to avoid writing the newline and indentation when we're in the root context. Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
1 parent 57c8b52 commit 5867d2c

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

impl/src/main/java/org/glassfish/json/JsonGeneratorImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ protected void writeComma() {
487487
currentContext.first = false;
488488
}
489489

490+
protected boolean inNone() {
491+
return currentContext.scope == Scope.IN_NONE;
492+
}
493+
490494
boolean isCommaAllowed() {
491495
return !currentContext.first && currentContext.scope != Scope.IN_FIELD;
492496
}

impl/src/main/java/org/glassfish/json/JsonPrettyGeneratorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private void writeIndent() {
8888
@Override
8989
protected void writeComma() {
9090
super.writeComma();
91-
if (isCommaAllowed()) {
91+
if (isCommaAllowed() && !inNone()) {
9292
writeChar('\n');
9393
writeIndent();
9494
}

impl/src/test/java/org/glassfish/json/tests/JsonGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void testPrettyPrinting() throws Exception {
225225
generator.close();
226226
writer.close();
227227

228-
BufferedReader reader = new BufferedReader(new StringReader(writer.toString().trim()));
228+
BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
229229
int numberOfLines = 0;
230230
String line;
231231
while ((line = reader.readLine()) != null) {

0 commit comments

Comments
 (0)