Top Description Fields Constructors Methods
com.google.gson

public Class FormattingStyle

extends Object
Class Inheritance
Imports
com.google.gson.stream.JsonWriter, java.util.Objects

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
GsonBuilder#setFormattingStyle(FormattingStyle), JsonWriter#setFormattingStyle(FormattingStyle), Wikipedia Newline article

Field Summary

Modifier and TypeField and Description
public static final FormattingStyle
COMPACT

The default compact formatting style:

  • no newline
  • no indent
  • no space after ',' and ':'
private final String
private final String
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
private final boolean

Constructor Summary

AccessConstructor and Description
private
FormattingStyle(String newline, String indent, boolean spaceAfterSeparators)

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the indent value.
getIndent
()

Returns the string value that will be used as indent.

public String

Returns:

the newline value.
getNewline
()

Returns the string value that will be used as a newline.

public boolean
usesSpaceAfterSeparators()

Returns whether a space will be used after ',' and ':'.

public FormattingStyle

Returns:

a newly created FormattingStyle
withIndent
(String
the string value that will be used as indent.
indent
)

Creates a FormattingStyle with the specified indent string.

public FormattingStyle

Returns:

a newly created FormattingStyle
withNewline
(String
the string value that will be used as newline.
newline
)

Creates a FormattingStyle with the specified newline setting.

public FormattingStyle

Returns:

a newly created FormattingStyle
withSpaceAfterSeparators
(boolean
whether to output a space after ',' and ':'.
spaceAfterSeparators
)

Creates a FormattingStyle which either uses a space after the separators ',' and ':' in the JSON output, or not.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPACTback to summary
public static final FormattingStyle COMPACT

The default compact formatting style:

  • no newline
  • no indent
  • no space after ',' and ':'
indentback to summary
private final String indent
newlineback to summary
private final String newline
PRETTYback to summary
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
spaceAfterSeparatorsback to summary
private final boolean spaceAfterSeparators

Constructor Detail

FormattingStyleback to summary
private FormattingStyle(String newline, String indent, boolean spaceAfterSeparators)

Method Detail

getIndentback to summary
public String getIndent()

Returns the string value that will be used as indent.

Returns:String

the indent value.

See Also
withIndent(String)
getNewlineback to summary
public String getNewline()

Returns the string value that will be used as a newline.

Returns:String

the newline value.

See Also
withNewline(String)
usesSpaceAfterSeparatorsback to summary
public boolean usesSpaceAfterSeparators()

Returns whether a space will be used after ',' and ':'.

See Also
withSpaceAfterSeparators(boolean)
withIndentback to summary
public FormattingStyle withIndent(String indent)

Creates a FormattingStyle with the specified indent string.

Only combinations of spaces and tabs are allowed in indent.

Parameters
indent:String

the string value that will be used as indent.

Returns:FormattingStyle

a newly created FormattingStyle

See Also
getIndent()
withNewlineback to summary
public FormattingStyle withNewline(String newline)

Creates a FormattingStyle 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 call java.lang.System#lineSeparator() to match the current OS.

Only combinations of \n and \r are allowed.

Parameters
newline:String

the string value that will be used as newline.

Returns:FormattingStyle

a newly created FormattingStyle

See Also
getNewline()
withSpaceAfterSeparatorsback to summary
public FormattingStyle withSpaceAfterSeparators(boolean spaceAfterSeparators)

Creates a FormattingStyle 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:boolean

whether to output a space after ',' and ':'.

Returns:FormattingStyle

a newly created FormattingStyle

See Also
usesSpaceAfterSeparators()