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 the FormattingStyle constructor (invoked via withNewline(...)) when the supplied newline string contains any character other than carriage return (\r) and line feed (\n). The validator runs the regex [\r\n]*, so only pure combinations of CR and LF are permitted; any other character (a space, a letter, a literal backslash) is rejected with IllegalArgumentException. FormattingStyle controls Gson's pretty-print output.
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 8b8628c656)
Solutions
- Pass actual newline characters only: "\n", "\r\n", "\r", or "" for compact output.
- If the value comes from config, map symbolic names ("LF", "CRLF", "CR") to the real characters before calling withNewline.
- Use System.lineSeparator() to match the OS, which is guaranteed to be CR/LF only.
- Validate the input with input.matches("[\r\n]*") and reject/sanitize early.
Example fix
// before: literal backslash-n string (two chars), not a newline
FormattingStyle s = FormattingStyle.PRETTY.withNewline("\\n"); // throws
// after: actual newline character
FormattingStyle s = FormattingStyle.PRETTY.withNewline("\n"); Defensive patterns
Strategy: validation
Validate before calling
// Map symbolic newline names to real characters before calling withNewline
static String resolveNewline(String spec) {
if (spec == null) return "";
switch (spec) {
case "LF": case "\n": return "\n";
case "CRLF": case "\r\n": return "\r\n";
case "CR": case "\r": return "\r";
case "": return "";
default: throw new IllegalArgumentException("Invalid newline spec: " + spec);
}
}
FormattingStyle s = FormattingStyle.PRETTY.withNewline(resolveNewline(configValue)); Type guard
static boolean isValidNewline(String s) {
return s != null && s.matches("[\\r\\n]*");
} Prevention
- Pass actual newline characters, not literal backslash-n strings.
- Use System.lineSeparator() to match the OS safely.
- Map config symbols (LF/CRLF/CR) to characters at a single boundary.
- Add a unit test asserting the configured FormattingStyle serializes a sample object without throwing.
When it happens
Trigger: Calling FormattingStyle.PRETTY.withNewline("\n\n") is fine, but withNewline("<br>") or withNewline("\\n") (a literal backslash-n two-character string) throws; passing System.lineSeparator() on a system that returns "\r\n" is fine, but a platform returning something exotic would throw; accidentally passing a non-newline whitespace like a space.
Common situations: Confusing the escape sequence \n (single LF char) with the literal two-character string "\n" (backslash + n); trying to use HTML or custom line separators for pretty printing; passing user-supplied newline config without sanitization; copy-paste from a config file that double-escapes.
Related errors
- Only combinations of spaces and tabs are allowed in indent.
- Invalid version: {version}
- The date pattern '{pattern}' is not valid
- Invalid style: {style}
- Invalid nesting limit: " + limit
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/d6571b9643d54e05.json.
Report an issue: GitHub.