Implementing code review feedback from r454: using isFirst instead of the Stack to keep track of whether to add commas or not.

This commit is contained in:
Inderjeet Singh 2009-10-01 00:57:55 +00:00
parent fbefa59b66
commit 77c2c29316

View File

@ -142,7 +142,6 @@ final class JsonPrintFormatter implements JsonFormatter {
} }
private class PrintFormattingVisitor implements JsonElementVisitor { private class PrintFormattingVisitor implements JsonElementVisitor {
private final Stack<Boolean> firstElementInLevel;
private final JsonWriter writer; private final JsonWriter writer;
private final Escaper escaper; private final Escaper escaper;
private final boolean serializeNulls; private final boolean serializeNulls;
@ -151,57 +150,49 @@ final class JsonPrintFormatter implements JsonFormatter {
this.writer = writer; this.writer = writer;
this.escaper = escaper; this.escaper = escaper;
this.serializeNulls = serializeNulls; this.serializeNulls = serializeNulls;
this.firstElementInLevel = new Stack<Boolean>();
} }
private void addCommaCheckingFirst() throws IOException { private void addCommaCheckingFirst(boolean first) throws IOException {
if (firstElementInLevel.peek()) { if (!first) {
// No longer first
firstElementInLevel.pop();
firstElementInLevel.push(false);
} else {
writer.elementSeparator(); writer.elementSeparator();
} }
} }
public void startArray(JsonArray array) throws IOException { public void startArray(JsonArray array) throws IOException {
firstElementInLevel.push(true);
writer.beginArray(); writer.beginArray();
} }
public void visitArrayMember(JsonArray parent, JsonPrimitive member, public void visitArrayMember(JsonArray parent, JsonPrimitive member,
boolean isFirst) throws IOException { boolean isFirst) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(isFirst);
writer.value(escapeJsonPrimitive(member)); writer.value(escapeJsonPrimitive(member));
} }
public void visitArrayMember(JsonArray parent, JsonArray member, public void visitArrayMember(JsonArray parent, JsonArray member,
boolean first) throws IOException { boolean first) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(first);
} }
public void visitArrayMember(JsonArray parent, JsonObject member, public void visitArrayMember(JsonArray parent, JsonObject member,
boolean first) throws IOException { boolean first) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(first);
} }
public void visitNullArrayMember(JsonArray parent, boolean isFirst) throws IOException { public void visitNullArrayMember(JsonArray parent, boolean isFirst) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(isFirst);
} }
public void endArray(JsonArray array) { public void endArray(JsonArray array) {
writer.endArray(); writer.endArray();
firstElementInLevel.pop();
} }
public void startObject(JsonObject object) throws IOException { public void startObject(JsonObject object) throws IOException {
firstElementInLevel.push(true);
writer.beginObject(); writer.beginObject();
} }
public void visitObjectMember(JsonObject parent, String memberName, JsonPrimitive member, public void visitObjectMember(JsonObject parent, String memberName, JsonPrimitive member,
boolean isFirst) throws IOException { boolean isFirst) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(isFirst);
writer.key(memberName); writer.key(memberName);
writer.fieldSeparator(); writer.fieldSeparator();
writer.value(escapeJsonPrimitive(member)); writer.value(escapeJsonPrimitive(member));
@ -209,14 +200,14 @@ final class JsonPrintFormatter implements JsonFormatter {
public void visitObjectMember(JsonObject parent, String memberName, JsonArray member, public void visitObjectMember(JsonObject parent, String memberName, JsonArray member,
boolean isFirst) throws IOException { boolean isFirst) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(isFirst);
writer.key(memberName); writer.key(memberName);
writer.fieldSeparator(); writer.fieldSeparator();
} }
public void visitObjectMember(JsonObject parent, String memberName, JsonObject member, public void visitObjectMember(JsonObject parent, String memberName, JsonObject member,
boolean isFirst) throws IOException { boolean isFirst) throws IOException {
addCommaCheckingFirst(); addCommaCheckingFirst(isFirst);
writer.key(memberName); writer.key(memberName);
writer.fieldSeparator(); writer.fieldSeparator();
} }
@ -230,7 +221,6 @@ final class JsonPrintFormatter implements JsonFormatter {
public void endObject(JsonObject object) { public void endObject(JsonObject object) {
writer.endObject(); writer.endObject();
firstElementInLevel.pop();
} }
public void visitPrimitive(JsonPrimitive primitive) throws IOException { public void visitPrimitive(JsonPrimitive primitive) throws IOException {