Incorporated comments from the code review r355

This commit is contained in:
Inderjeet Singh 2008-12-30 19:03:43 +00:00
parent 05f54f3552
commit e839336eea
2 changed files with 8 additions and 19 deletions

View File

@ -68,6 +68,7 @@ public final class GsonBuilder {
private int dateStyle; private int dateStyle;
private int timeStyle; private int timeStyle;
private boolean serializeSpecialFloatingPointValues; private boolean serializeSpecialFloatingPointValues;
private boolean escapeHtmlChars;
/** /**
* 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
@ -80,6 +81,7 @@ public final class GsonBuilder {
ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS; ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS;
serializeLongAsString = false; serializeLongAsString = false;
serializeInnerClasses = true; serializeInnerClasses = 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;
@ -202,32 +204,19 @@ 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());
return this;
}
/**
* Configures Gson to output Json that fits in a page for pretty printing. This option only
* affects Json serialization.
*
* @param escapeHtmlChars true if specific HTML characters should be escaped
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3
*/
public GsonBuilder setPrettyPrinting(boolean escapeHtmlChars) {
setFormatter(new JsonPrintFormatter(escapeHtmlChars)); setFormatter(new JsonPrintFormatter(escapeHtmlChars));
return this; return this;
} }
/** /**
* Configures Gson to output Json in a compact format. * By default, Gson escapes HTML characters such as < > etc. Use this option to configure
* Gson to pass-through HTML characters as is.
* *
* @param escapeHtmlChars true if specific HTML characters should be escaped
* @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
* @since 1.3 * @since 1.3
*/ */
public GsonBuilder setCompactPrinting(boolean escapeHtmlChars) { public GsonBuilder disableHtmlEscaping() {
setFormatter(new JsonCompactFormatter(escapeHtmlChars)); this.escapeHtmlChars = false;
return this; return this;
} }

View File

@ -553,7 +553,7 @@ public class PrimitiveTest extends TestCase {
String result = gson.toJson(target); String result = gson.toJson(target);
assertFalse(result.equals('"' + target + '"')); assertFalse(result.equals('"' + target + '"'));
gson = new GsonBuilder().setCompactPrinting(false).create(); gson = new GsonBuilder().disableHtmlEscaping().create();
result = gson.toJson(target); result = gson.toJson(target);
assertTrue(result.equals('"' + target + '"')); assertTrue(result.equals('"' + target + '"'));
} }