From 77c2c29316dd1d8fc7ea608dd6f67585bb6b006c Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 1 Oct 2009 00:57:55 +0000 Subject: [PATCH] Implementing code review feedback from r454: using isFirst instead of the Stack to keep track of whether to add commas or not. --- .../com/google/gson/JsonPrintFormatter.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonPrintFormatter.java b/gson/src/main/java/com/google/gson/JsonPrintFormatter.java index 39961582..c5054212 100644 --- a/gson/src/main/java/com/google/gson/JsonPrintFormatter.java +++ b/gson/src/main/java/com/google/gson/JsonPrintFormatter.java @@ -142,7 +142,6 @@ final class JsonPrintFormatter implements JsonFormatter { } private class PrintFormattingVisitor implements JsonElementVisitor { - private final Stack firstElementInLevel; private final JsonWriter writer; private final Escaper escaper; private final boolean serializeNulls; @@ -151,57 +150,49 @@ final class JsonPrintFormatter implements JsonFormatter { this.writer = writer; this.escaper = escaper; this.serializeNulls = serializeNulls; - this.firstElementInLevel = new Stack(); } - private void addCommaCheckingFirst() throws IOException { - if (firstElementInLevel.peek()) { - // No longer first - firstElementInLevel.pop(); - firstElementInLevel.push(false); - } else { + private void addCommaCheckingFirst(boolean first) throws IOException { + if (!first) { writer.elementSeparator(); } } public void startArray(JsonArray array) throws IOException { - firstElementInLevel.push(true); writer.beginArray(); } public void visitArrayMember(JsonArray parent, JsonPrimitive member, boolean isFirst) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(isFirst); writer.value(escapeJsonPrimitive(member)); } public void visitArrayMember(JsonArray parent, JsonArray member, boolean first) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(first); } public void visitArrayMember(JsonArray parent, JsonObject member, boolean first) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(first); } public void visitNullArrayMember(JsonArray parent, boolean isFirst) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(isFirst); } public void endArray(JsonArray array) { writer.endArray(); - firstElementInLevel.pop(); } public void startObject(JsonObject object) throws IOException { - firstElementInLevel.push(true); writer.beginObject(); } public void visitObjectMember(JsonObject parent, String memberName, JsonPrimitive member, boolean isFirst) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(isFirst); writer.key(memberName); writer.fieldSeparator(); writer.value(escapeJsonPrimitive(member)); @@ -209,14 +200,14 @@ final class JsonPrintFormatter implements JsonFormatter { public void visitObjectMember(JsonObject parent, String memberName, JsonArray member, boolean isFirst) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(isFirst); writer.key(memberName); writer.fieldSeparator(); } public void visitObjectMember(JsonObject parent, String memberName, JsonObject member, boolean isFirst) throws IOException { - addCommaCheckingFirst(); + addCommaCheckingFirst(isFirst); writer.key(memberName); writer.fieldSeparator(); } @@ -230,7 +221,6 @@ final class JsonPrintFormatter implements JsonFormatter { public void endObject(JsonObject object) { writer.endObject(); - firstElementInLevel.pop(); } public void visitPrimitive(JsonPrimitive primitive) throws IOException {