Restore pretty printing. The pretty printing format isn't as compact as the previous format - for example arrays of integers are printed one-per-line, whereas the previous format compacted these to all sit on the same line.

This commit is contained in:
Jesse Wilson 2010-11-01 23:03:41 +00:00
parent 43f2a0012b
commit dea7ab89fe
6 changed files with 27 additions and 19 deletions

View File

@ -110,8 +110,8 @@ public final class Gson {
private final boolean serializeNulls;
private final boolean htmlSafe;
private final boolean generateNonExecutableJson;
private final boolean prettyPrinting;
/**
* Constructs a Gson object with default configuration. The default configuration has the
@ -151,14 +151,14 @@ public final class Gson {
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
false, DefaultTypeAdapters.getDefaultSerializers(),
DefaultTypeAdapters.getDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true);
DefaultTypeAdapters.getDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true, false);
}
Gson(ExclusionStrategy serializationStrategy, ExclusionStrategy deserializationStrategy,
FieldNamingStrategy2 fieldNamingPolicy, MappedObjectConstructor objectConstructor,
boolean serializeNulls, ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
boolean generateNonExecutableGson, boolean htmlSafe) {
FieldNamingStrategy2 fieldNamingPolicy, MappedObjectConstructor objectConstructor,
boolean serializeNulls, ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting) {
this.serializationStrategy = serializationStrategy;
this.deserializationStrategy = deserializationStrategy;
this.fieldNamingPolicy = fieldNamingPolicy;
@ -168,6 +168,7 @@ public final class Gson {
this.deserializers = deserializers;
this.generateNonExecutableJson = generateNonExecutableGson;
this.htmlSafe = htmlSafe;
this.prettyPrinting = prettyPrinting;
}
private ObjectNavigatorFactory createDefaultObjectNavigatorFactory(ExclusionStrategy strategy) {
@ -349,7 +350,11 @@ public final class Gson {
if (generateNonExecutableJson) {
writer.append(JSON_NON_EXECUTABLE_PREFIX);
}
toJson(jsonElement, new JsonWriter(Streams.writerForAppendable(writer)));
JsonWriter jsonWriter = new JsonWriter(Streams.writerForAppendable(writer));
if (prettyPrinting) {
jsonWriter.setIndent(" ");
}
toJson(jsonElement, jsonWriter);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -544,7 +544,7 @@ public final class GsonBuilder {
Gson gson = new Gson(serializationExclusionStrategy, deserializationExclusionStrategy,
fieldNamingPolicy, objConstructor, serializeNulls, customSerializers,
customDeserializers, generateNonExecutableJson, escapeHtmlChars);
customDeserializers, generateNonExecutableJson, escapeHtmlChars, prettyPrinting);
return gson;
}

View File

@ -42,7 +42,8 @@ public class FunctionWithInternalDependenciesTest extends TestCase {
Gson gson = new Gson(exclusionStrategy, exclusionStrategy, Gson.DEFAULT_NAMING_POLICY,
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
false, DefaultTypeAdapters.getDefaultSerializers(),
DefaultTypeAdapters.getDefaultDeserializers(), Gson.DEFAULT_JSON_NON_EXECUTABLE, true);
DefaultTypeAdapters.getDefaultDeserializers(), Gson.DEFAULT_JSON_NON_EXECUTABLE, true,
false);
assertEquals("{}", gson.toJson(new ClassWithNoFields() {
// empty anonymous class
}));

View File

@ -323,11 +323,11 @@ public class MapTest extends TestCase {
map.put("c", new HashMap<String, Object>());
assertEquals("{\"a\":12,\"b\":null,\"c\":{}}",
new GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(map));
new GsonBuilder().serializeNulls().create().toJson(map));
assertEquals("{\"a\":12,\"b\":null,\"c\":{}}",
new GsonBuilder().serializeNulls().create().toJson(map));
assertEquals("{\"a\":12,\"c\":{}}",
new GsonBuilder().setPrettyPrinting().create().toJson(map));
new GsonBuilder().create().toJson(map));
assertEquals("{\"a\":12,\"c\":{}}",
new GsonBuilder().create().toJson(map));
}

View File

@ -129,7 +129,7 @@ public class NullObjectAndFieldTest extends TestCase {
public void testPrintPrintingObjectWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setPrettyPrinting().create();
Gson gson = gsonBuilder.create();
String result = gson.toJson(new ClassWithMembers());
assertEquals("{}", result);
@ -140,7 +140,7 @@ public class NullObjectAndFieldTest extends TestCase {
public void testPrintPrintingArraysWithNulls() throws Exception {
gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.setPrettyPrinting().create();
Gson gson = gsonBuilder.create();
String result = gson.toJson(new String[] { "1", null, "3" });
assertEquals("[\"1\",null,\"3\"]", result);

View File

@ -70,21 +70,23 @@ public class PrettyPrintingTest extends TestCase {
public void testPrettyPrintArrayOfPrimitives() {
int[] ints = new int[] { 1, 2, 3, 4, 5 };
String json = gson.toJson(ints);
assertEquals("[1,2,3,4,5]", json);
assertEquals("[\n 1,\n 2,\n 3,\n 4,\n 5\n]", json);
}
public void testPrettyPrintArrayOfPrimitiveArrays() {
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]]", json);
assertEquals("[\n [\n 1,\n 2\n ],\n [\n 3,\n 4\n ],\n [\n 5,\n 6\n ],"
+ "\n [\n 7,\n 8\n ],\n [\n 9,\n 0\n ],\n [\n 10\n ]\n]", json);
}
public void testPrettyPrintListOfPrimitiveArrays() {
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]]", json);
assertEquals("[\n [\n 1,\n 2\n ],\n [\n 3,\n 4\n ],\n [\n 5,\n 6\n ],"
+ "\n [\n 7,\n 8\n ],\n [\n 9,\n 0\n ],\n [\n 10\n ]\n]", json);
}
public void testMap() {
@ -92,7 +94,7 @@ public class PrettyPrintingTest extends TestCase {
map.put("abc", 1);
map.put("def", 5);
String json = gson.toJson(map);
assertEquals("{\"abc\":1,\"def\":5}", json);
assertEquals("{\n \"abc\": 1,\n \"def\": 5\n}", json);
}
// In response to bug 153
@ -100,7 +102,7 @@ public class PrettyPrintingTest extends TestCase {
ClassWithMap obj = new ClassWithMap();
obj.map = new LinkedHashMap<String, Integer>();
String json = gson.toJson(obj);
assertTrue(json.contains("{\"map\":{},\"value\":2}"));
assertTrue(json.contains("{\n \"map\": {},\n \"value\": 2\n}"));
}
@SuppressWarnings("unused")
@ -112,7 +114,7 @@ public class PrettyPrintingTest extends TestCase {
public void testMultipleArrays() {
int[][][] ints = new int[][][] { { { 1 }, { 2 } } };
String json = gson.toJson(ints);
assertEquals("[[[1],[2]]]", json);
assertEquals("[\n [\n [\n 1\n ],\n [\n 2\n ]\n ]\n]", json);
}
private void print(String msg) {