Skip to content

Commit f49271c

Browse files
Verdentlukasj
authored andcommitted
Yasson Issue #89 - Pretty formatting (#87)
Generator of pretty formatting now prints key and value on the same line Issue link: eclipse-ee4j/yasson#89 Signed-off-by: David Kral <[email protected]>
1 parent d5bded4 commit f49271c

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -481,12 +481,16 @@ public JsonGenerator writeEnd() {
481481
}
482482

483483
protected void writeComma() {
484-
if (!currentContext.first && currentContext.scope != Scope.IN_FIELD) {
484+
if (isCommaAllowed()) {
485485
writeChar(',');
486486
}
487487
currentContext.first = false;
488488
}
489489

490+
boolean isCommaAllowed() {
491+
return !currentContext.first && currentContext.scope != Scope.IN_FIELD;
492+
}
493+
490494
protected void writeColon() {
491495
writeChar(':');
492496
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -88,8 +88,10 @@ private void writeIndent() {
8888
@Override
8989
protected void writeComma() {
9090
super.writeComma();
91-
writeChar('\n');
92-
writeIndent();
91+
if (isCommaAllowed()) {
92+
writeChar('\n');
93+
writeIndent();
94+
}
9395
}
9496

9597
@Override

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -211,6 +211,32 @@ public void testPrettyObjectWriter() throws Exception {
211211
JsonObjectTest.testPerson(person);
212212
}
213213

214+
public void testPrettyPrinting() throws Exception {
215+
String[][] lines = {{"firstName", "John"}, {"lastName", "Smith"}};
216+
StringWriter writer = new StringWriter();
217+
Map<String, Object> config = new HashMap<>();
218+
config.put(JsonGenerator.PRETTY_PRINTING, true);
219+
JsonGenerator generator = Json.createGeneratorFactory(config)
220+
.createGenerator(writer);
221+
generator.writeStartObject()
222+
.write("firstName", "John")
223+
.write("lastName", "Smith")
224+
.writeEnd();
225+
generator.close();
226+
writer.close();
227+
228+
BufferedReader reader = new BufferedReader(new StringReader(writer.toString().trim()));
229+
int numberOfLines = 0;
230+
String line;
231+
while ((line = reader.readLine()) != null) {
232+
numberOfLines++;
233+
if (numberOfLines > 1 && numberOfLines < 4) {
234+
assertTrue(line.contains("\"" + lines[numberOfLines - 2][0] + "\": \"" + lines[numberOfLines - 2][1] + "\""));
235+
}
236+
}
237+
assertEquals(4, numberOfLines);
238+
}
239+
214240
public void testPrettyObjectStream() throws Exception {
215241
ByteArrayOutputStream out = new ByteArrayOutputStream();
216242
Map<String, Object> config = new HashMap<>();

0 commit comments

Comments
 (0)