Fix pretty printing of interlacing arrays and objects.
This commit is contained in:
parent
9ad4b2bc7e
commit
1cad54f7b1
@ -130,7 +130,8 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class PrintFormattingVisitor implements JsonElementVisitor {
|
private class PrintFormattingVisitor implements JsonElementVisitor {
|
||||||
private final Map<Integer, Boolean> first;
|
private final Map<Integer, Boolean> firstArrayElement;
|
||||||
|
private final Map<Integer, Boolean> firstObjectMember;
|
||||||
private final JsonWriter writer;
|
private final JsonWriter writer;
|
||||||
private final boolean serializeNulls;
|
private final boolean serializeNulls;
|
||||||
private int level = 0;
|
private int level = 0;
|
||||||
@ -138,10 +139,11 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
PrintFormattingVisitor(JsonWriter writer, boolean serializeNulls) {
|
PrintFormattingVisitor(JsonWriter writer, boolean serializeNulls) {
|
||||||
this.writer = writer;
|
this.writer = writer;
|
||||||
this.serializeNulls = serializeNulls;
|
this.serializeNulls = serializeNulls;
|
||||||
this.first = new HashMap<Integer, Boolean>();
|
this.firstArrayElement = new HashMap<Integer, Boolean>();
|
||||||
|
this.firstObjectMember = new HashMap<Integer, Boolean>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCommaCheckingFirst() {
|
private void addCommaCheckingFirst(Map<Integer, Boolean> first) {
|
||||||
if (first.get(level) != Boolean.FALSE) {
|
if (first.get(level) != Boolean.FALSE) {
|
||||||
first.put(level, false);
|
first.put(level, false);
|
||||||
} else {
|
} else {
|
||||||
@ -150,25 +152,25 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startArray(JsonArray array) {
|
public void startArray(JsonArray array) {
|
||||||
first.put(++level, true);
|
firstArrayElement.put(++level, true);
|
||||||
writer.beginArray();
|
writer.beginArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitArrayMember(JsonArray parent, JsonPrimitive member, boolean isFirst) {
|
public void visitArrayMember(JsonArray parent, JsonPrimitive member, boolean isFirst) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstArrayElement);
|
||||||
writer.value(member.toString());
|
writer.value(member.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitArrayMember(JsonArray parent, JsonArray member, boolean first) {
|
public void visitArrayMember(JsonArray parent, JsonArray member, boolean first) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstArrayElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitArrayMember(JsonArray parent, JsonObject member, boolean first) {
|
public void visitArrayMember(JsonArray parent, JsonObject member, boolean first) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstArrayElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitNullArrayMember(JsonArray parent, boolean isFirst) {
|
public void visitNullArrayMember(JsonArray parent, boolean isFirst) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstArrayElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endArray(JsonArray array) {
|
public void endArray(JsonArray array) {
|
||||||
@ -177,12 +179,13 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startObject(JsonObject object) {
|
public void startObject(JsonObject object) {
|
||||||
|
firstObjectMember.put(level, 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) {
|
boolean isFirst) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstObjectMember);
|
||||||
writer.key(memberName);
|
writer.key(memberName);
|
||||||
writer.fieldSeparator();
|
writer.fieldSeparator();
|
||||||
writer.value(member.toString());
|
writer.value(member.toString());
|
||||||
@ -190,14 +193,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) {
|
boolean isFirst) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstObjectMember);
|
||||||
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) {
|
boolean isFirst) {
|
||||||
addCommaCheckingFirst();
|
addCommaCheckingFirst(firstObjectMember);
|
||||||
writer.key(memberName);
|
writer.key(memberName);
|
||||||
writer.fieldSeparator();
|
writer.fieldSeparator();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user