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 FormattingStyle when the indent string contains anything other than spaces and tabs (validated by the regex [ \t]*). Invoked via withIndent(...); an illegal indent rejects the style. This guarantees indentation can never inject arbitrary content into the JSON output.

Solutions

  1. Use only ASCII space (0x20) and tab (0x09) in the indent, e.g. withIndent(" ") or withIndent("\t").
  2. Validate/sanitize the config value to spaces and tabs before constructing the FormattingStyle.
  3. If a richer separator is needed, apply it as a post-processing step on the serialized output.

Example fix

// before
style.withIndent("\u00a0\u00a0"); // non-breaking space -> throws

// after
style.withIndent("  ");
Defensive patterns

Strategy: validation

Validate before calling

// Validate indent before constructing the style
static String safeIndent(String s) {
  if (s == null || !s.matches("[ \\t]*")) {
    throw new IllegalArgumentException("indent may only contain spaces and tabs");
  }
  return s;
}
style.withIndent(safeIndent(configured));

Prevention

When it happens

Trigger: Calling withIndent("\t "); is fine, but withIndent("--") or withIndent("\u00a0") (non-breaking space) throws; building indent from user-supplied config with stray characters; accidental inclusion of a newline in the indent.

Common situations: Config-driven indent strings pulled from YAML/properties with unexpected characters; copy-paste introducing unicode whitespace; trying to use a multi-character decorator as indent.

Related errors


AI-assisted analysis of google/gson@310ac341f2 (2026-08-10). Data as JSON: /api/errors/e89ca2bd15fde35e. Report an issue: GitHub.

Appendix: 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 310ac341f2)