Minor fixes for pretty printing.

This commit is contained in:
Joel Leitch 2009-01-11 05:01:23 +00:00
parent 02decace26
commit 90a4619034
3 changed files with 56 additions and 27 deletions

View File

@ -64,14 +64,16 @@ final class JsonPrintFormatter implements JsonFormatter {
line = new StringBuilder();
}
void key(String key) {
void key(String key) throws IOException {
breakLineIfThisToNextExceedsLimit(key.length() + 2);
getLine().append('"');
getLine().append(key);
getLine().append('"');
}
void value(String value) {
getLine().append(value);
void value(String value) throws IOException {
breakLineIfThisToNextExceedsLimit(value.length() + 2);
getLine().append(value);
}
void fieldSeparator() throws IOException {
@ -85,9 +87,9 @@ final class JsonPrintFormatter implements JsonFormatter {
}
void beginObject() throws IOException {
++level;
breakLineIfNeeded();
getLine().append('{');
++level;
}
void endObject() {
@ -96,9 +98,9 @@ final class JsonPrintFormatter implements JsonFormatter {
}
void beginArray() throws IOException {
++level;
breakLineIfNeeded();
getLine().append('[');
++level;
}
void endArray() {
@ -107,10 +109,14 @@ final class JsonPrintFormatter implements JsonFormatter {
}
private void breakLineIfNeeded() throws IOException {
if (getLine().length() > printMargin - rightMargin) {
finishLine();
}
breakLineIfThisToNextExceedsLimit(0);
}
private void breakLineIfThisToNextExceedsLimit(int nextLength) throws IOException {
if (getLine().length() + nextLength > printMargin - rightMargin) {
finishLine();
}
}
private void finishLine() throws IOException {
if (line != null) {
@ -222,15 +228,15 @@ final class JsonPrintFormatter implements JsonFormatter {
}
}
public void endObject(JsonObject object) {
public void endObject(JsonObject object) throws IOException {
writer.endObject();
}
public void visitPrimitive(JsonPrimitive primitive) {
public void visitPrimitive(JsonPrimitive primitive) throws IOException {
writer.value(primitive.toString());
}
public void visitNull() {
public void visitNull() throws IOException {
writer.value("null");
}
}

View File

@ -102,10 +102,6 @@ public class NullObjectAndFieldTest extends TestCase {
assertNull(target.value);
}
private static class ClassWithNullWrappedPrimitive {
private Long value;
}
public void testExplicitSerializationOfNullCollectionMembers() {
Gson gson = gsonBuilder.create();
ClassWithMembers target = new ClassWithMembers();
@ -119,12 +115,6 @@ public class NullObjectAndFieldTest extends TestCase {
String json = gson.toJson(target);
assertTrue(json.contains("\"str\":null"));
}
static class ClassWithMembers {
String str;
int[] array;
Collection<String> col;
}
public void testCustomSerializationOfNulls() {
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
@ -134,7 +124,39 @@ public class NullObjectAndFieldTest extends TestCase {
String expected = "{\"bag\":null}";
assertEquals(expected, actual);
}
public void testPrintPrintingObjectWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setPrettyPrinting().create();
String result = gson.toJson(new ClassWithMembers());
assertEquals("{}\n", result);
gson = gsonBuilder.serializeNulls().create();
result = gson.toJson(new ClassWithMembers());
assertTrue(result.contains("\"str\":null"));
}
public void testPrintPrintingArraysWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setPrettyPrinting().create();
String result = gson.toJson(new String[] { "1", null, "3" });
assertEquals("[\"1\",null,\"3\"]\n", result);
gson = gsonBuilder.serializeNulls().create();
result = gson.toJson(new String[] { "1", null, "3" });
assertEquals("[\"1\",null,\"3\"]\n", result);
}
private static class ClassWithNullWrappedPrimitive {
private Long value;
}
private static class ClassWithMembers {
String str;
int[] array;
Collection<String> col;
}
private static class ClassWithObjectsSerializer implements JsonSerializer<ClassWithObjects> {
public JsonElement serialize(ClassWithObjects src, Type typeOfSrc,
JsonSerializationContext context) {

View File

@ -35,7 +35,7 @@ import java.util.List;
* @author Joel Leitch
*/
public class PrettyPrintingTest extends TestCase {
private static int PRINT_MARGIN = 100;
private static int PRINT_MARGIN = 80;
private static int RIGHT_MARGIN = 4;
private static boolean DEBUG = false;
@ -74,20 +74,21 @@ public class PrettyPrintingTest extends TestCase {
}
public void testPrettyPrintArrayOfPrimitiveArrays() {
int[][] ints = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 0 }, { 10 } };
int[][] ints = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
{ 9, 0 }, { 10 } };
String json = gson.toJson(ints);
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
}
public void testPrettyPrintListOfPrimitiveArrays() {
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
{ 9, 0 }, { 10 } });
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 },
{ 5, 6 }, { 7, 8 }, { 9, 0 }, { 10 } });
String json = gson.toJson(list);
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
}
public void testMultipleArrays() {
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
String json = gson.toJson(ints);
assertEquals("[[[1],[2]]]\n", json);
}
@ -106,7 +107,7 @@ public class PrettyPrintingTest extends TestCase {
if (c == '\n') {
position = 0;
}
assertTrue(position < PRINT_MARGIN + RIGHT_MARGIN);
assertTrue(position <= PRINT_MARGIN - RIGHT_MARGIN + 1);
}
}
}