Minor fixes for pretty printing.
This commit is contained in:
parent
02decace26
commit
90a4619034
@ -64,14 +64,16 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
line = new StringBuilder();
|
line = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
void key(String key) {
|
void key(String key) throws IOException {
|
||||||
|
breakLineIfThisToNextExceedsLimit(key.length() + 2);
|
||||||
getLine().append('"');
|
getLine().append('"');
|
||||||
getLine().append(key);
|
getLine().append(key);
|
||||||
getLine().append('"');
|
getLine().append('"');
|
||||||
}
|
}
|
||||||
|
|
||||||
void value(String value) {
|
void value(String value) throws IOException {
|
||||||
getLine().append(value);
|
breakLineIfThisToNextExceedsLimit(value.length() + 2);
|
||||||
|
getLine().append(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fieldSeparator() throws IOException {
|
void fieldSeparator() throws IOException {
|
||||||
@ -85,9 +87,9 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void beginObject() throws IOException {
|
void beginObject() throws IOException {
|
||||||
++level;
|
|
||||||
breakLineIfNeeded();
|
breakLineIfNeeded();
|
||||||
getLine().append('{');
|
getLine().append('{');
|
||||||
|
++level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void endObject() {
|
void endObject() {
|
||||||
@ -96,9 +98,9 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void beginArray() throws IOException {
|
void beginArray() throws IOException {
|
||||||
++level;
|
|
||||||
breakLineIfNeeded();
|
breakLineIfNeeded();
|
||||||
getLine().append('[');
|
getLine().append('[');
|
||||||
|
++level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void endArray() {
|
void endArray() {
|
||||||
@ -107,11 +109,15 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void breakLineIfNeeded() throws IOException {
|
private void breakLineIfNeeded() throws IOException {
|
||||||
if (getLine().length() > printMargin - rightMargin) {
|
breakLineIfThisToNextExceedsLimit(0);
|
||||||
finishLine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void breakLineIfThisToNextExceedsLimit(int nextLength) throws IOException {
|
||||||
|
if (getLine().length() + nextLength > printMargin - rightMargin) {
|
||||||
|
finishLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void finishLine() throws IOException {
|
private void finishLine() throws IOException {
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
writer.append(line).append("\n");
|
writer.append(line).append("\n");
|
||||||
@ -222,15 +228,15 @@ final class JsonPrintFormatter implements JsonFormatter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endObject(JsonObject object) {
|
public void endObject(JsonObject object) throws IOException {
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitPrimitive(JsonPrimitive primitive) {
|
public void visitPrimitive(JsonPrimitive primitive) throws IOException {
|
||||||
writer.value(primitive.toString());
|
writer.value(primitive.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void visitNull() {
|
public void visitNull() throws IOException {
|
||||||
writer.value("null");
|
writer.value("null");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,10 +102,6 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||||||
assertNull(target.value);
|
assertNull(target.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ClassWithNullWrappedPrimitive {
|
|
||||||
private Long value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testExplicitSerializationOfNullCollectionMembers() {
|
public void testExplicitSerializationOfNullCollectionMembers() {
|
||||||
Gson gson = gsonBuilder.create();
|
Gson gson = gsonBuilder.create();
|
||||||
ClassWithMembers target = new ClassWithMembers();
|
ClassWithMembers target = new ClassWithMembers();
|
||||||
@ -120,12 +116,6 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||||||
assertTrue(json.contains("\"str\":null"));
|
assertTrue(json.contains("\"str\":null"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ClassWithMembers {
|
|
||||||
String str;
|
|
||||||
int[] array;
|
|
||||||
Collection<String> col;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCustomSerializationOfNulls() {
|
public void testCustomSerializationOfNulls() {
|
||||||
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
|
gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer());
|
||||||
Gson gson = gsonBuilder.create();
|
Gson gson = gsonBuilder.create();
|
||||||
@ -135,6 +125,38 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||||||
assertEquals(expected, actual);
|
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> {
|
private static class ClassWithObjectsSerializer implements JsonSerializer<ClassWithObjects> {
|
||||||
public JsonElement serialize(ClassWithObjects src, Type typeOfSrc,
|
public JsonElement serialize(ClassWithObjects src, Type typeOfSrc,
|
||||||
JsonSerializationContext context) {
|
JsonSerializationContext context) {
|
||||||
|
@ -35,7 +35,7 @@ import java.util.List;
|
|||||||
* @author Joel Leitch
|
* @author Joel Leitch
|
||||||
*/
|
*/
|
||||||
public class PrettyPrintingTest extends TestCase {
|
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 int RIGHT_MARGIN = 4;
|
||||||
|
|
||||||
private static boolean DEBUG = false;
|
private static boolean DEBUG = false;
|
||||||
@ -74,20 +74,21 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintArrayOfPrimitiveArrays() {
|
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);
|
String json = gson.toJson(ints);
|
||||||
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
|
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintListOfPrimitiveArrays() {
|
public void testPrettyPrintListOfPrimitiveArrays() {
|
||||||
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
|
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 },
|
||||||
{ 9, 0 }, { 10 } });
|
{ 5, 6 }, { 7, 8 }, { 9, 0 }, { 10 } });
|
||||||
String json = gson.toJson(list);
|
String json = gson.toJson(list);
|
||||||
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
|
assertEquals("[[1,2],[3,4],[5,6],[7,8],[9,0],[10]]\n", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMultipleArrays() {
|
public void testMultipleArrays() {
|
||||||
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
|
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
|
||||||
String json = gson.toJson(ints);
|
String json = gson.toJson(ints);
|
||||||
assertEquals("[[[1],[2]]]\n", json);
|
assertEquals("[[[1],[2]]]\n", json);
|
||||||
}
|
}
|
||||||
@ -106,7 +107,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
position = 0;
|
position = 0;
|
||||||
}
|
}
|
||||||
assertTrue(position < PRINT_MARGIN + RIGHT_MARGIN);
|
assertTrue(position <= PRINT_MARGIN - RIGHT_MARGIN + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user