google/gson · error · IllegalArgumentException
Only combinations of \n and \r are allowed in newline.
Error message
Only combinations of \n and \r are allowed in newline.
What it means
Thrown by FormattingStyle when the newline string contains any character other than '\n' and '\r' (validated by the regex [\r\n]*). The FormattingStyle constructor is invoked by withNewline(...), so any illegal newline value rejects the whole style. This keeps serialized output strictly line-break controlled.
Solutions
- Use only \r and \n in the newline string, e.g. withNewline("\r\n") or withNewline("\n").
- Use System.lineSeparator() to match the OS, which is guaranteed to be a \r\n or \n combination.
- Sanitize the value: strip any character that is not \r or \n before passing it.
- If you need a different separator, post-process the serialized JSON instead of forcing it through FormattingStyle.
Example fix
// before
style.withNewline("\n<br>"); // throws
// after
style.withNewline("\r\n"); Defensive patterns
Strategy: validation
Validate before calling
// Validate newline before constructing the style
static String safeNewline(String s) {
if (s == null || !s.matches("[\\r\\n]*")) {
throw new IllegalArgumentException("newline may only contain \\r and \\n");
}
return s;
}
style.withNewline(safeNewline(configured)); Prevention
- Prefer System.lineSeparator() for OS-correct values.
- Sanitize config-driven newline to [\r\n] only.
- Unit-test FormattingStyle construction with the configured values.
When it happens
Trigger: Calling FormattingStyle.PRETTY.withNewline("\r\n<br>"); passing a newline containing a literal space, NUL, or platform-specific control char; building the newline from user input that includes stray characters.
Common situations: Trying to use HTML line breaks or custom markers as newline; concatenating OS line separator with extra whitespace; reading newline config from a properties file that includes trailing spaces; accidental inclusion of a BOM or form-feed.
Related errors
- Only combinations of spaces and tabs are allowed in indent.
- Invalid style
- Invalid version
- Cannot set value of 'static final
- Class does not implement any supported type adapter class…
AI-assisted analysis of google/gson@310ac341f2 (2026-08-10).
Data as JSON: /api/errors/d6571b9643d54e05.
Report an issue: GitHub.
Appendix: source
Thrown at gson/src/main/java/com/google/gson/FormattingStyle.java:70
*/
public static final FormattingStyle COMPACT = new FormattingStyle("", "", false);
/**
* The default pretty printing formatting style:
*
* <ul>
* <li>{@code "\n"} as newline
* <li>two spaces as indent
* <li>a space between {@code ':'} and the subsequent value
* </ul>
*/
public static final FormattingStyle PRETTY = new FormattingStyle("\n", " ", true);
private FormattingStyle(String newline, String indent, boolean spaceAfterSeparators) {
Objects.requireNonNull(newline, "newline == null");
Objects.requireNonNull(indent, "indent == null");
if (!newline.matches("[\r\n]*")) {
throw new IllegalArgumentException(
"Only combinations of \\n and \\r are allowed in newline.");
}
if (!indent.matches("[ \t]*")) {
throw new IllegalArgumentException(
"Only combinations of spaces and tabs are allowed in indent.");
}
this.newline = newline;
this.indent = indent;
this.spaceAfterSeparators = spaceAfterSeparators;
}
/**
* Creates a {@link FormattingStyle} with the specified newline setting.
*
* <p>It can be used to accommodate certain OS convention, for example hardcode {@code "\n"} for
* Linux and macOS, {@code "\r\n"} for Windows, or call {@link java.lang.System#lineSeparator()}
* to match the current OS.
*View on GitHub (pinned to 310ac341f2)