JamesNK/Newtonsoft.Json · error · ArgumentException

Invalid JavaScript string quote character. Valid quote…

Error message

Invalid JavaScript string quote character. Valid quote characters are ' and ".

What it means

Thrown by the JsonTextWriter.QuoteChar setter when assigning a character other than double-quote (") or single-quote ('). Valid JSON permits only these two as string/property delimiters, so any other char (backtick, space, etc.) is rejected with ArgumentException at configuration time rather than producing malformed output.

Solutions

  1. Use '"' (default, maximally compatible) or '\'' for single-quoted output.
  2. Validate config before assignment: if (c == '\"' || c == '\'') writer.QuoteChar = c;.
  3. For single-quoted JS-style output set both QuoteChar='\'' and StringEscapeHandling.EscapeSingleQuotes (as appropriate).

Example fix

// before
writer.QuoteChar = '`';

// after
writer.QuoteChar = '"';
Defensive patterns

Strategy: validation

Validate before calling

if (configuredQuote == '"' || configuredQuote == '\'')
{
    writer.QuoteChar = configuredQuote;
}

Type guard

static bool IsValidQuoteChar(char c) => c == '"' || c == '\'';

Prevention

When it happens

Trigger: Setting writer.QuoteChar to any char except " or '; reading QuoteChar from config as a char/string and assigning an invalid value; passing a unicode whitespace or punctuation char.

Common situations: Config that supplies a wrong quote character; interop code assuming backticks are valid; parsing a 'quote style' setting into a char incorrectly.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/dc5c6688ce63ed9a. Report an issue: GitHub.

Appendix: source

Thrown at Src/Newtonsoft.Json/JsonTextWriter.cs:114

                {
                    throw new ArgumentException("Indentation value must be greater than 0.");
                }

                _indentation = value;
            }
        }

        /// <summary>
        /// Gets or sets which character to use to quote attribute values.
        /// </summary>
        public char QuoteChar
        {
            get => _quoteChar;
            set
            {
                if (value != '"' && value != '\'')
                {
                    throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
                }

                _quoteChar = value;
                UpdateCharEscapeFlags();
            }
        }

        /// <summary>
        /// Gets or sets which character to use for indenting when <see cref="JsonWriter.Formatting"/> is set to <see cref="Formatting.Indented"/>.
        /// </summary>
        public char IndentChar
        {
            get => _indentChar;
            set
            {
                if (value != _indentChar)
                {
                    _indentChar = value;

View on GitHub (pinned to 4f73e74372)