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:
parent
43f2a0012b
commit
dea7ab89fe
@ -110,8 +110,8 @@ public final class Gson {
|
|||||||
|
|
||||||
private final boolean serializeNulls;
|
private final boolean serializeNulls;
|
||||||
private final boolean htmlSafe;
|
private final boolean htmlSafe;
|
||||||
|
|
||||||
private final boolean generateNonExecutableJson;
|
private final boolean generateNonExecutableJson;
|
||||||
|
private final boolean prettyPrinting;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a Gson object with default configuration. The default configuration has the
|
* 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,
|
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
|
||||||
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
||||||
false, DefaultTypeAdapters.getDefaultSerializers(),
|
false, DefaultTypeAdapters.getDefaultSerializers(),
|
||||||
DefaultTypeAdapters.getDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true);
|
DefaultTypeAdapters.getDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson(ExclusionStrategy serializationStrategy, ExclusionStrategy deserializationStrategy,
|
Gson(ExclusionStrategy serializationStrategy, ExclusionStrategy deserializationStrategy,
|
||||||
FieldNamingStrategy2 fieldNamingPolicy, MappedObjectConstructor objectConstructor,
|
FieldNamingStrategy2 fieldNamingPolicy, MappedObjectConstructor objectConstructor,
|
||||||
boolean serializeNulls, ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
boolean serializeNulls, ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
||||||
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
||||||
boolean generateNonExecutableGson, boolean htmlSafe) {
|
boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting) {
|
||||||
this.serializationStrategy = serializationStrategy;
|
this.serializationStrategy = serializationStrategy;
|
||||||
this.deserializationStrategy = deserializationStrategy;
|
this.deserializationStrategy = deserializationStrategy;
|
||||||
this.fieldNamingPolicy = fieldNamingPolicy;
|
this.fieldNamingPolicy = fieldNamingPolicy;
|
||||||
@ -168,6 +168,7 @@ public final class Gson {
|
|||||||
this.deserializers = deserializers;
|
this.deserializers = deserializers;
|
||||||
this.generateNonExecutableJson = generateNonExecutableGson;
|
this.generateNonExecutableJson = generateNonExecutableGson;
|
||||||
this.htmlSafe = htmlSafe;
|
this.htmlSafe = htmlSafe;
|
||||||
|
this.prettyPrinting = prettyPrinting;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectNavigatorFactory createDefaultObjectNavigatorFactory(ExclusionStrategy strategy) {
|
private ObjectNavigatorFactory createDefaultObjectNavigatorFactory(ExclusionStrategy strategy) {
|
||||||
@ -349,7 +350,11 @@ public final class Gson {
|
|||||||
if (generateNonExecutableJson) {
|
if (generateNonExecutableJson) {
|
||||||
writer.append(JSON_NON_EXECUTABLE_PREFIX);
|
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) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -544,7 +544,7 @@ public final class GsonBuilder {
|
|||||||
|
|
||||||
Gson gson = new Gson(serializationExclusionStrategy, deserializationExclusionStrategy,
|
Gson gson = new Gson(serializationExclusionStrategy, deserializationExclusionStrategy,
|
||||||
fieldNamingPolicy, objConstructor, serializeNulls, customSerializers,
|
fieldNamingPolicy, objConstructor, serializeNulls, customSerializers,
|
||||||
customDeserializers, generateNonExecutableJson, escapeHtmlChars);
|
customDeserializers, generateNonExecutableJson, escapeHtmlChars, prettyPrinting);
|
||||||
return gson;
|
return gson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,8 @@ public class FunctionWithInternalDependenciesTest extends TestCase {
|
|||||||
Gson gson = new Gson(exclusionStrategy, exclusionStrategy, Gson.DEFAULT_NAMING_POLICY,
|
Gson gson = new Gson(exclusionStrategy, exclusionStrategy, Gson.DEFAULT_NAMING_POLICY,
|
||||||
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
||||||
false, DefaultTypeAdapters.getDefaultSerializers(),
|
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() {
|
assertEquals("{}", gson.toJson(new ClassWithNoFields() {
|
||||||
// empty anonymous class
|
// empty anonymous class
|
||||||
}));
|
}));
|
||||||
|
@ -323,11 +323,11 @@ public class MapTest extends TestCase {
|
|||||||
map.put("c", new HashMap<String, Object>());
|
map.put("c", new HashMap<String, Object>());
|
||||||
|
|
||||||
assertEquals("{\"a\":12,\"b\":null,\"c\":{}}",
|
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\":{}}",
|
assertEquals("{\"a\":12,\"b\":null,\"c\":{}}",
|
||||||
new GsonBuilder().serializeNulls().create().toJson(map));
|
new GsonBuilder().serializeNulls().create().toJson(map));
|
||||||
assertEquals("{\"a\":12,\"c\":{}}",
|
assertEquals("{\"a\":12,\"c\":{}}",
|
||||||
new GsonBuilder().setPrettyPrinting().create().toJson(map));
|
new GsonBuilder().create().toJson(map));
|
||||||
assertEquals("{\"a\":12,\"c\":{}}",
|
assertEquals("{\"a\":12,\"c\":{}}",
|
||||||
new GsonBuilder().create().toJson(map));
|
new GsonBuilder().create().toJson(map));
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrintPrintingObjectWithNulls() throws Exception {
|
public void testPrintPrintingObjectWithNulls() throws Exception {
|
||||||
gsonBuilder = new GsonBuilder();
|
gsonBuilder = new GsonBuilder();
|
||||||
Gson gson = gsonBuilder.setPrettyPrinting().create();
|
Gson gson = gsonBuilder.create();
|
||||||
String result = gson.toJson(new ClassWithMembers());
|
String result = gson.toJson(new ClassWithMembers());
|
||||||
assertEquals("{}", result);
|
assertEquals("{}", result);
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ public class NullObjectAndFieldTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrintPrintingArraysWithNulls() throws Exception {
|
public void testPrintPrintingArraysWithNulls() throws Exception {
|
||||||
gsonBuilder = new GsonBuilder();
|
gsonBuilder = new GsonBuilder();
|
||||||
Gson gson = gsonBuilder.setPrettyPrinting().create();
|
Gson gson = gsonBuilder.create();
|
||||||
String result = gson.toJson(new String[] { "1", null, "3" });
|
String result = gson.toJson(new String[] { "1", null, "3" });
|
||||||
assertEquals("[\"1\",null,\"3\"]", result);
|
assertEquals("[\"1\",null,\"3\"]", result);
|
||||||
|
|
||||||
|
@ -70,21 +70,23 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
public void testPrettyPrintArrayOfPrimitives() {
|
public void testPrettyPrintArrayOfPrimitives() {
|
||||||
int[] ints = new int[] { 1, 2, 3, 4, 5 };
|
int[] ints = new int[] { 1, 2, 3, 4, 5 };
|
||||||
String json = gson.toJson(ints);
|
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() {
|
public void testPrettyPrintArrayOfPrimitiveArrays() {
|
||||||
int[][] ints = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
|
int[][] ints = new int[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 },
|
||||||
{ 9, 0 }, { 10 } };
|
{ 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]]", 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() {
|
public void testPrettyPrintListOfPrimitiveArrays() {
|
||||||
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 },
|
List<Integer[]> list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 },
|
||||||
{ 5, 6 }, { 7, 8 }, { 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]]", 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() {
|
public void testMap() {
|
||||||
@ -92,7 +94,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
map.put("abc", 1);
|
map.put("abc", 1);
|
||||||
map.put("def", 5);
|
map.put("def", 5);
|
||||||
String json = gson.toJson(map);
|
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
|
// In response to bug 153
|
||||||
@ -100,7 +102,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
ClassWithMap obj = new ClassWithMap();
|
ClassWithMap obj = new ClassWithMap();
|
||||||
obj.map = new LinkedHashMap<String, Integer>();
|
obj.map = new LinkedHashMap<String, Integer>();
|
||||||
String json = gson.toJson(obj);
|
String json = gson.toJson(obj);
|
||||||
assertTrue(json.contains("{\"map\":{},\"value\":2}"));
|
assertTrue(json.contains("{\n \"map\": {},\n \"value\": 2\n}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@ -112,7 +114,7 @@ public class PrettyPrintingTest extends TestCase {
|
|||||||
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]]]", json);
|
assertEquals("[\n [\n [\n 1\n ],\n [\n 2\n ]\n ]\n]", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(String msg) {
|
private void print(String msg) {
|
||||||
|
Loading…
Reference in New Issue
Block a user