{"record":{"id":"d6571b9643d54e05","repo":"google/gson","slug":"only-combinations-of-n-and-r-are-allowed-in-newl","errorCode":null,"errorMessage":"Only combinations of \\n and \\r are allowed in newline.","messagePattern":"Only combinations of \\\\n and \\\\r are allowed in newline\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/FormattingStyle.java","lineNumber":70,"sourceCode":"   */\n  public static final FormattingStyle COMPACT = new FormattingStyle(\"\", \"\", false);\n\n  /**\n   * The default pretty printing formatting style:\n   *\n   * <ul>\n   *   <li>{@code \"\\n\"} as newline\n   *   <li>two spaces as indent\n   *   <li>a space between {@code ':'} and the subsequent value\n   * </ul>\n   */\n  public static final FormattingStyle PRETTY = new FormattingStyle(\"\\n\", \"  \", true);\n\n  private FormattingStyle(String newline, String indent, boolean spaceAfterSeparators) {\n    Objects.requireNonNull(newline, \"newline == null\");\n    Objects.requireNonNull(indent, \"indent == null\");\n    if (!newline.matches(\"[\\r\\n]*\")) {\n      throw new IllegalArgumentException(\n          \"Only combinations of \\\\n and \\\\r are allowed in newline.\");\n    }\n    if (!indent.matches(\"[ \\t]*\")) {\n      throw new IllegalArgumentException(\n          \"Only combinations of spaces and tabs are allowed in indent.\");\n    }\n    this.newline = newline;\n    this.indent = indent;\n    this.spaceAfterSeparators = spaceAfterSeparators;\n  }\n\n  /**\n   * Creates a {@link FormattingStyle} with the specified newline setting.\n   *\n   * <p>It can be used to accommodate certain OS convention, for example hardcode {@code \"\\n\"} for\n   * Linux and macOS, {@code \"\\r\\n\"} for Windows, or call {@link java.lang.System#lineSeparator()}\n   * to match the current OS.\n   *","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/FormattingStyle.java#L52-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nstyle.withNewline(\"\\n<br>\"); // throws\n\n// after\nstyle.withNewline(\"\\r\\n\");","handlingStrategy":"validation","validationCode":"// Validate newline before constructing the style\nstatic String safeNewline(String s) {\n  if (s == null || !s.matches(\"[\\\\r\\\\n]*\")) {\n    throw new IllegalArgumentException(\"newline may only contain \\\\r and \\\\n\");\n  }\n  return s;\n}\nstyle.withNewline(safeNewline(configured));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer System.lineSeparator() for OS-correct values.","Sanitize config-driven newline to [\\r\\n] only.","Unit-test FormattingStyle construction with the configured values."],"tags":["formatting-style","pretty-printing","configuration","validation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}