incorporated feedback from r358
This commit is contained in:
parent
e839336eea
commit
3690d362b9
@ -58,7 +58,6 @@ public final class GsonBuilder {
|
|||||||
private final AnonymousAndLocalClassExclusionStrategy anonAndLocalClassExclusionStrategy;
|
private final AnonymousAndLocalClassExclusionStrategy anonAndLocalClassExclusionStrategy;
|
||||||
private final InnerClassExclusionStrategy innerClassExclusionStrategy;
|
private final InnerClassExclusionStrategy innerClassExclusionStrategy;
|
||||||
private boolean excludeFieldsWithoutExposeAnnotation;
|
private boolean excludeFieldsWithoutExposeAnnotation;
|
||||||
private JsonFormatter formatter;
|
|
||||||
private FieldNamingStrategy fieldNamingPolicy;
|
private FieldNamingStrategy fieldNamingPolicy;
|
||||||
private final ParameterizedTypeHandlerMap<InstanceCreator<?>> instanceCreators;
|
private final ParameterizedTypeHandlerMap<InstanceCreator<?>> instanceCreators;
|
||||||
private final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers;
|
private final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers;
|
||||||
@ -69,6 +68,7 @@ public final class GsonBuilder {
|
|||||||
private int timeStyle;
|
private int timeStyle;
|
||||||
private boolean serializeSpecialFloatingPointValues;
|
private boolean serializeSpecialFloatingPointValues;
|
||||||
private boolean escapeHtmlChars;
|
private boolean escapeHtmlChars;
|
||||||
|
private boolean prettyPrinting;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a GsonBuilder instance that can be used to build Gson with various configuration
|
* 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;
|
ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS;
|
||||||
serializeLongAsString = false;
|
serializeLongAsString = false;
|
||||||
serializeInnerClasses = true;
|
serializeInnerClasses = true;
|
||||||
|
prettyPrinting = false;
|
||||||
escapeHtmlChars = true;
|
escapeHtmlChars = true;
|
||||||
anonAndLocalClassExclusionStrategy = new AnonymousAndLocalClassExclusionStrategy();
|
anonAndLocalClassExclusionStrategy = new AnonymousAndLocalClassExclusionStrategy();
|
||||||
innerClassExclusionStrategy = new InnerClassExclusionStrategy();
|
innerClassExclusionStrategy = new InnerClassExclusionStrategy();
|
||||||
modifierBasedExclusionStrategy = Gson.DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY;
|
modifierBasedExclusionStrategy = Gson.DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY;
|
||||||
excludeFieldsWithoutExposeAnnotation = false;
|
excludeFieldsWithoutExposeAnnotation = false;
|
||||||
formatter = Gson.DEFAULT_JSON_FORMATTER;
|
|
||||||
fieldNamingPolicy = Gson.DEFAULT_NAMING_POLICY;
|
fieldNamingPolicy = Gson.DEFAULT_NAMING_POLICY;
|
||||||
instanceCreators = new ParameterizedTypeHandlerMap<InstanceCreator<?>>();
|
instanceCreators = new ParameterizedTypeHandlerMap<InstanceCreator<?>>();
|
||||||
serializers = new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
|
serializers = new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
|
||||||
@ -204,7 +204,7 @@ public final class GsonBuilder {
|
|||||||
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
||||||
*/
|
*/
|
||||||
public GsonBuilder setPrettyPrinting() {
|
public GsonBuilder setPrettyPrinting() {
|
||||||
setFormatter(new JsonPrintFormatter(escapeHtmlChars));
|
prettyPrinting = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,19 +220,6 @@ public final class GsonBuilder {
|
|||||||
return this;
|
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
|
* 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
|
* 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());
|
customInstanceCreators.registerIfAbsent(DefaultTypeAdapters.getDefaultInstanceCreators());
|
||||||
MappedObjectConstructor objConstructor = Gson.createObjectConstructor(customInstanceCreators);
|
MappedObjectConstructor objConstructor = Gson.createObjectConstructor(customInstanceCreators);
|
||||||
|
|
||||||
|
JsonFormatter formatter = prettyPrinting ?
|
||||||
|
new JsonPrintFormatter(escapeHtmlChars) : new JsonCompactFormatter(escapeHtmlChars);
|
||||||
Gson gson = new Gson(exclusionStrategy, fieldNamingPolicy, objConstructor,
|
Gson gson = new Gson(exclusionStrategy, fieldNamingPolicy, objConstructor,
|
||||||
formatter, serializeNulls, customSerializers, customDeserializers);
|
formatter, serializeNulls, customSerializers, customDeserializers);
|
||||||
return gson;
|
return gson;
|
||||||
|
@ -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
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
@ -13,56 +13,42 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.ArrayOfObjects;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||||
import com.google.gson.common.TestTypes.ClassWithNoFields;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functional tests for Gson that depend on some internal package-protected elements of
|
* Functional tests for pretty printing option.
|
||||||
* 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 Inderjeet Singh
|
||||||
* @author Joel Leitch
|
* @author Joel Leitch
|
||||||
*/
|
*/
|
||||||
public class FunctionalWithInternalDependenciesTest extends TestCase {
|
public class PrettyPrintingTest extends TestCase {
|
||||||
private static int INDENTATION_SIZE = 2;
|
|
||||||
private static int PRINT_MARGIN = 100;
|
private static int PRINT_MARGIN = 100;
|
||||||
private static int RIGHT_MARGIN = 8;
|
private static int RIGHT_MARGIN = 4;
|
||||||
|
|
||||||
private static boolean DEBUG = false;
|
private static boolean DEBUG = false;
|
||||||
|
|
||||||
private GsonBuilder builder;
|
private Gson gson;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
builder = new GsonBuilder();
|
gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintList() {
|
public void testPrettyPrintList() {
|
||||||
JsonFormatter formatter = new JsonPrintFormatter(
|
|
||||||
PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true);
|
|
||||||
Gson gson = builder.setFormatter(formatter).create();
|
|
||||||
BagOfPrimitives b = new BagOfPrimitives();
|
BagOfPrimitives b = new BagOfPrimitives();
|
||||||
List<BagOfPrimitives> listOfB = new LinkedList<BagOfPrimitives>();
|
List<BagOfPrimitives> listOfB = new LinkedList<BagOfPrimitives>();
|
||||||
for (int i = 0; i < 15; ++i) {
|
for (int i = 0; i < 15; ++i) {
|
||||||
@ -75,9 +61,6 @@ public class FunctionalWithInternalDependenciesTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintArrayOfObjects() {
|
public void testPrettyPrintArrayOfObjects() {
|
||||||
JsonFormatter formatter = new JsonPrintFormatter(
|
|
||||||
PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true);
|
|
||||||
Gson gson = builder.setFormatter(formatter).create();
|
|
||||||
ArrayOfObjects target = new ArrayOfObjects();
|
ArrayOfObjects target = new ArrayOfObjects();
|
||||||
String json = gson.toJson(target);
|
String json = gson.toJson(target);
|
||||||
print(json);
|
print(json);
|
||||||
@ -85,27 +68,18 @@ public class FunctionalWithInternalDependenciesTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintArrayOfPrimitives() {
|
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 };
|
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]\n", json);
|
assertEquals("[1,2,3,4,5]\n", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPrettyPrintArrayOfPrimitiveArrays() {
|
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 } };
|
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() {
|
||||||
JsonFormatter formatter = new JsonPrintFormatter(
|
|
||||||
PRINT_MARGIN, INDENTATION_SIZE, RIGHT_MARGIN, true);
|
|
||||||
Gson gson = builder.setFormatter(formatter).create();
|
|
||||||
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 }, { 5, 6 }, { 7, 8 },
|
||||||
{ 9, 0 }, { 10 } });
|
{ 9, 0 }, { 10 } });
|
||||||
String json = gson.toJson(list);
|
String json = gson.toJson(list);
|
||||||
@ -113,9 +87,6 @@ public class FunctionalWithInternalDependenciesTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testMultipleArrays() {
|
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 } } };
|
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);
|
Loading…
Reference in New Issue
Block a user