From 3690d362b9e3007e390d38092ce339e8a1145781 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Tue, 30 Dec 2008 22:42:36 +0000 Subject: [PATCH] incorporated feedback from r358 --- .../java/com/google/gson/GsonBuilder.java | 21 +++------ .../FunctionWithInternalDependenciesTest.java | 26 +++++++++++ .../PrettyPrintingTest.java} | 45 ++++--------------- 3 files changed, 39 insertions(+), 53 deletions(-) create mode 100644 gson/src/test/java/com/google/gson/FunctionWithInternalDependenciesTest.java rename gson/src/test/java/com/google/gson/{FunctionalWithInternalDependenciesTest.java => functional/PrettyPrintingTest.java} (61%) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index a57eae9f..8e0d3c95 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -58,7 +58,6 @@ public final class GsonBuilder { private final AnonymousAndLocalClassExclusionStrategy anonAndLocalClassExclusionStrategy; private final InnerClassExclusionStrategy innerClassExclusionStrategy; private boolean excludeFieldsWithoutExposeAnnotation; - private JsonFormatter formatter; private FieldNamingStrategy fieldNamingPolicy; private final ParameterizedTypeHandlerMap> instanceCreators; private final ParameterizedTypeHandlerMap> serializers; @@ -69,6 +68,7 @@ public final class GsonBuilder { private int timeStyle; private boolean serializeSpecialFloatingPointValues; private boolean escapeHtmlChars; + private boolean prettyPrinting; /** * Creates a GsonBuilder instance that can be used to build Gson with various configuration @@ -81,12 +81,12 @@ public final class GsonBuilder { ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS; serializeLongAsString = false; serializeInnerClasses = true; + prettyPrinting = false; escapeHtmlChars = true; anonAndLocalClassExclusionStrategy = new AnonymousAndLocalClassExclusionStrategy(); innerClassExclusionStrategy = new InnerClassExclusionStrategy(); modifierBasedExclusionStrategy = Gson.DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY; excludeFieldsWithoutExposeAnnotation = false; - formatter = Gson.DEFAULT_JSON_FORMATTER; fieldNamingPolicy = Gson.DEFAULT_NAMING_POLICY; instanceCreators = new ParameterizedTypeHandlerMap>(); serializers = new ParameterizedTypeHandlerMap>(); @@ -204,7 +204,7 @@ public final class GsonBuilder { * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder setPrettyPrinting() { - setFormatter(new JsonPrintFormatter(escapeHtmlChars)); + prettyPrinting = true; return this; } @@ -220,19 +220,6 @@ public final class GsonBuilder { return this; } - /** - * Configures Gson with a new formatting strategy other than the default strategy. The default - * strategy is to provide a compact representation that eliminates all unneeded white-space. - * - * @param formatter the new formatter to use. - * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern - * @see JsonPrintFormatter - */ - GsonBuilder setFormatter(JsonFormatter formatter) { - this.formatter = formatter; - return this; - } - /** * Configures Gson to serialize {@code Date} objects according to the pattern provided. You can * call this method or {@link #setDateFormat(int)} multiple times, but only the last invocation @@ -431,6 +418,8 @@ public final class GsonBuilder { customInstanceCreators.registerIfAbsent(DefaultTypeAdapters.getDefaultInstanceCreators()); MappedObjectConstructor objConstructor = Gson.createObjectConstructor(customInstanceCreators); + JsonFormatter formatter = prettyPrinting ? + new JsonPrintFormatter(escapeHtmlChars) : new JsonCompactFormatter(escapeHtmlChars); Gson gson = new Gson(exclusionStrategy, fieldNamingPolicy, objConstructor, formatter, serializeNulls, customSerializers, customDeserializers); return gson; diff --git a/gson/src/test/java/com/google/gson/FunctionWithInternalDependenciesTest.java b/gson/src/test/java/com/google/gson/FunctionWithInternalDependenciesTest.java new file mode 100644 index 00000000..3aab2e20 --- /dev/null +++ b/gson/src/test/java/com/google/gson/FunctionWithInternalDependenciesTest.java @@ -0,0 +1,26 @@ +package com.google.gson; + +import com.google.gson.common.TestTypes.ClassWithNoFields; + +import junit.framework.TestCase; + +import java.lang.reflect.Modifier; + +/** + * Functional tests for Gson that depend on some internal package-protected elements of + * com.google.gson package and hence must be placed in the same package. We should make every + * attempt to migrate tests out of this class. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +public class FunctionWithInternalDependenciesTest extends TestCase { + + public void testAnonymousLocalClassesSerialization() { + Gson gson = new Gson(new ModifierBasedExclusionStrategy( + true, Modifier.TRANSIENT, Modifier.STATIC), Gson.DEFAULT_NAMING_POLICY); + assertEquals("{}", gson.toJson(new ClassWithNoFields() { + // empty anonymous class + })); + } +} diff --git a/gson/src/test/java/com/google/gson/FunctionalWithInternalDependenciesTest.java b/gson/src/test/java/com/google/gson/functional/PrettyPrintingTest.java similarity index 61% rename from gson/src/test/java/com/google/gson/FunctionalWithInternalDependenciesTest.java rename to gson/src/test/java/com/google/gson/functional/PrettyPrintingTest.java index 44540f4f..ebd13273 100644 --- a/gson/src/test/java/com/google/gson/FunctionalWithInternalDependenciesTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrettyPrintingTest.java @@ -13,56 +13,42 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.gson; +package com.google.gson.functional; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.common.TestTypes.ArrayOfObjects; import com.google.gson.common.TestTypes.BagOfPrimitives; -import com.google.gson.common.TestTypes.ClassWithNoFields; import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; -import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** - * Functional tests for Gson that depend on some internal package-protected elements of - * com.google.gson package and hence must be placed in the same package. We should make every - * attempt to migrate tests out of this class. + * Functional tests for pretty printing option. * * @author Inderjeet Singh * @author Joel Leitch */ -public class FunctionalWithInternalDependenciesTest extends TestCase { - private static int INDENTATION_SIZE = 2; +public class PrettyPrintingTest extends TestCase { private static int PRINT_MARGIN = 100; - private static int RIGHT_MARGIN = 8; + private static int RIGHT_MARGIN = 4; private static boolean DEBUG = false; - private GsonBuilder builder; + private Gson gson; @Override protected void setUp() throws Exception { super.setUp(); - builder = new GsonBuilder(); - } - - public void testAnonymousLocalClassesSerialization() { - Gson gson = new Gson(new ModifierBasedExclusionStrategy( - true, Modifier.TRANSIENT, Modifier.STATIC), Gson.DEFAULT_NAMING_POLICY); - assertEquals("{}", gson.toJson(new ClassWithNoFields() { - // empty anonymous class - })); + gson = new GsonBuilder().setPrettyPrinting().create(); } public void testPrettyPrintList() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); BagOfPrimitives b = new BagOfPrimitives(); List listOfB = new LinkedList(); for (int i = 0; i < 15; ++i) { @@ -75,9 +61,6 @@ public class FunctionalWithInternalDependenciesTest extends TestCase { } public void testPrettyPrintArrayOfObjects() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); ArrayOfObjects target = new ArrayOfObjects(); String json = gson.toJson(target); print(json); @@ -85,27 +68,18 @@ public class FunctionalWithInternalDependenciesTest extends TestCase { } public void testPrettyPrintArrayOfPrimitives() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); int[] ints = new int[] { 1, 2, 3, 4, 5 }; String json = gson.toJson(ints); assertEquals("[1,2,3,4,5]\n", json); } public void testPrettyPrintArrayOfPrimitiveArrays() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); 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() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); List list = Arrays.asList(new Integer[][] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 0 }, { 10 } }); String json = gson.toJson(list); @@ -113,9 +87,6 @@ public class FunctionalWithInternalDependenciesTest extends TestCase { } public void testMultipleArrays() { - JsonFormatter formatter = new JsonPrintFormatter( - PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true); - Gson gson = builder.setFormatter(formatter).create(); int[][][] ints = new int[][][] { { { 1 }, { 2 } } }; String json = gson.toJson(ints); assertEquals("[[[1],[2]]]\n", json);