google/gson · error · IllegalArgumentException

Only combinations of spaces and tabs are allowed in indent.

Error message

Only combinations of spaces and tabs are allowed in indent.

What it means

Thrown by the FormattingStyle constructor (invoked via withIndent(...)) when the supplied indent string contains any character other than space (' ') and tab ('\t'). The validator runs the regex [ \t]*, so only combinations of spaces and tabs are allowed as indentation; any other character (a letter, a digit, a newline, a literal backslash) is rejected with IllegalArgumentException.

Source

Thrown at gson/src/main/java/com/google/gson/FormattingStyle.java:74

   * 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.
   *
   * <p>Only combinations of {@code \n} and {@code \r} are allowed.
   *
   * @param newline the string value that will be used as newline.
   * @return a newly created {@link FormattingStyle}

View on GitHub (pinned to 8b8628c656)

Solutions

  1. Pass only spaces and/or tabs: " ", "\t", " \t", etc.
  2. If indent comes from config, map symbolic names ("tab", "2spaces", "4spaces") to real characters.
  3. Validate with input.matches("[ \t]*") before calling withIndent.
  4. Watch for non-breaking spaces (U+00A0) copied from rich-text editors; replace with normal spaces.

Example fix

// before: literal backslash-t or non-whitespace indent
FormattingStyle s = FormattingStyle.PRETTY.withIndent("--"); // throws

// after: spaces and tabs only
FormattingStyle s = FormattingStyle.PRETTY.withIndent("  ");
Defensive patterns

Strategy: validation

Validate before calling

// Map symbolic indent names and validate before withIndent
static String resolveIndent(String spec) {
  if (spec == null) return "";
  switch (spec) {
    case "tab": return "\t";
    case "2spaces": return "  ";
    case "4spaces": return "    ";
    case "": return "";
    default:
      if (!spec.matches("[ \\t]*")) throw new IllegalArgumentException("Invalid indent: " + spec);
      return spec;
  }
}
FormattingStyle s = FormattingStyle.PRETTY.withIndent(resolveIndent(configValue));

Type guard

static boolean isValidIndent(String s) {
  return s != null && s.matches("[ \\t]*");
}

Prevention

When it happens

Trigger: Calling FormattingStyle.PRETTY.withIndent(" ") is fine, but withIndent("--") or withIndent("\t-") or withIndent("\\t") (literal backslash-t) throws; trying to use a visible indent marker for debugging; passing a newline-containing string; user-supplied indent config with stray characters.

Common situations: Confusing the escape \t with the literal two-character string "\t"; using visual debug markers as indent; copy-paste from config that double-escapes tabs; passing whitespace that is actually a non-breaking space (U+00A0), which is neither ' ' nor '\t'.

Related errors


AI-assisted analysis of google/gson@8b8628c656 (2026-08-04). Data as JSON: /data/errors/e89ca2bd15fde35e.json. Report an issue: GitHub.