- java.lang.Object
-
- com.google.gson.FormattingStyle
-
public class FormattingStyle extends Object
A class used to control what the serialization output looks like.It currently has the following configuration methods, but more methods might be added in the future:
-
-
Field Summary
Fields Modifier and Type Field Description static FormattingStyleCOMPACTThe default compact formatting style: no newline no indent no space after','and':'static FormattingStylePRETTYThe default pretty printing formatting style:"\n"as newline two spaces as indent a space between':'and the subsequent value
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetIndent()Returns the string value that will be used as indent.StringgetNewline()Returns the string value that will be used as a newline.booleanusesSpaceAfterSeparators()Returns whether a space will be used after','and':'.FormattingStylewithIndent(String indent)Creates aFormattingStylewith the specified indent string.FormattingStylewithNewline(String newline)Creates aFormattingStylewith the specified newline setting.FormattingStylewithSpaceAfterSeparators(boolean spaceAfterSeparators)Creates aFormattingStylewhich either uses a space after the separators','and':'in the JSON output, or not.
-
-
-
Field Detail
-
COMPACT
public static final FormattingStyle COMPACT
The default compact formatting style:- no newline
- no indent
- no space after
','and':'
-
PRETTY
public static final FormattingStyle PRETTY
The default pretty printing formatting style:"\n"as newline- two spaces as indent
- a space between
':'and the subsequent value
-
-
Method Detail
-
withNewline
public FormattingStyle withNewline(String newline)
Creates aFormattingStylewith the specified newline setting.It can be used to accommodate certain OS convention, for example hardcode
"\n"for Linux and macOS,"\r\n"for Windows, or callSystem.lineSeparator()to match the current OS.Only combinations of
\nand\rare allowed.- Parameters:
newline- the string value that will be used as newline.- Returns:
- a newly created
FormattingStyle
-
withIndent
public FormattingStyle withIndent(String indent)
Creates aFormattingStylewith the specified indent string.Only combinations of spaces and tabs allowed in indent.
- Parameters:
indent- the string value that will be used as indent.- Returns:
- a newly created
FormattingStyle
-
withSpaceAfterSeparators
public FormattingStyle withSpaceAfterSeparators(boolean spaceAfterSeparators)
Creates aFormattingStylewhich either uses a space after the separators','and':'in the JSON output, or not.This setting has no effect on the configured newline. If a non-empty newline is configured, it will always be added after
','and no space is added after the','in that case.- Parameters:
spaceAfterSeparators- whether to output a space after','and':'.- Returns:
- a newly created
FormattingStyle
-
getNewline
public String getNewline()
Returns the string value that will be used as a newline.- Returns:
- the newline value.
-
getIndent
public String getIndent()
Returns the string value that will be used as indent.- Returns:
- the indent value.
-
usesSpaceAfterSeparators
public boolean usesSpaceAfterSeparators()
Returns whether a space will be used after','and':'.
-
-