{"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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/FormattingStyle.java#L52-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: literal backslash-n string (two chars), not a newline\nFormattingStyle s = FormattingStyle.PRETTY.withNewline(\"\\\\n\"); // throws\n\n// after: actual newline character\nFormattingStyle s = FormattingStyle.PRETTY.withNewline(\"\\n\");","handlingStrategy":"validation","validationCode":"// Map symbolic newline names to real characters before calling withNewline\nstatic String resolveNewline(String spec) {\n  if (spec == null) return \"\";\n  switch (spec) {\n    case \"LF\": case \"\\n\": return \"\\n\";\n    case \"CRLF\": case \"\\r\\n\": return \"\\r\\n\";\n    case \"CR\": case \"\\r\": return \"\\r\";\n    case \"\": return \"\";\n    default: throw new IllegalArgumentException(\"Invalid newline spec: \" + spec);\n  }\n}\nFormattingStyle s = FormattingStyle.PRETTY.withNewline(resolveNewline(configValue));","typeGuard":"static boolean isValidNewline(String s) {\n  return s != null && s.matches(\"[\\\\r\\\\n]*\");\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["formatting","newline","pretty-print","configuration","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}