Package com.google.gson
Class FormattingStyle
java.lang.Object
com.google.gson.FormattingStyle
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:
- Since:
- 2.11.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FormattingStyle
The default compact formatting style: no newline no indent no space after','
and':'
private final String
private final String
static final FormattingStyle
The default pretty printing formatting style:"\n"
as newline two spaces as indent a space between':'
and the subsequent valueprivate final boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
FormattingStyle
(String newline, String indent, boolean spaceAfterSeparators) -
Method Summary
Modifier and TypeMethodDescriptionReturns the string value that will be used as indent.Returns the string value that will be used as a newline.boolean
Returns whether a space will be used after','
and':'
.withIndent
(String indent) Creates aFormattingStyle
with the specified indent string.withNewline
(String newline) Creates aFormattingStyle
with the specified newline setting.withSpaceAfterSeparators
(boolean spaceAfterSeparators) Creates aFormattingStyle
which either uses a space after the separators','
and':'
in the JSON output, or not.
-
Field Details
-
newline
-
indent
-
spaceAfterSeparators
private final boolean spaceAfterSeparators -
COMPACT
The default compact formatting style:- no newline
- no indent
- no space after
','
and':'
-
PRETTY
The default pretty printing formatting style:"\n"
as newline- two spaces as indent
- a space between
':'
and the subsequent value
-
-
Constructor Details
-
FormattingStyle
-
-
Method Details
-
withNewline
Creates aFormattingStyle
with 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
\n
and\r
are allowed.- Parameters:
newline
- the string value that will be used as newline.- Returns:
- a newly created
FormattingStyle
- See Also:
-
withIndent
Creates aFormattingStyle
with the specified indent string.Only combinations of spaces and tabs are allowed in indent.
- Parameters:
indent
- the string value that will be used as indent.- Returns:
- a newly created
FormattingStyle
- See Also:
-
withSpaceAfterSeparators
Creates aFormattingStyle
which 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
- See Also:
-
getNewline
Returns the string value that will be used as a newline.- Returns:
- the newline value.
- See Also:
-
getIndent
Returns the string value that will be used as indent.- Returns:
- the indent value.
- See Also:
-
usesSpaceAfterSeparators
public boolean usesSpaceAfterSeparators()Returns whether a space will be used after','
and':'
.- See Also:
-