JamesNK/Newtonsoft.Json · error · ArgumentException

Delimiter must be a single or double quote.

Error message

Delimiter must be a single or double quote.

What it means

Thrown by JsonConvert.ToString(string, char, StringEscapeHandling) when the delimiter argument is neither a single quote (') nor a double quote ("). A JSON string must be delimited by one of these two characters; any other char is invalid. It is an ArgumentException.

Source

Thrown at Src/Newtonsoft.Json/JsonConvert.cs:446

        /// <param name="delimiter">The string delimiter character.</param>
        /// <returns>A JSON string representation of the <see cref="String"/>.</returns>
        public static string ToString(string? value, char delimiter)
        {
            return ToString(value, delimiter, StringEscapeHandling.Default);
        }

        /// <summary>
        /// Converts the <see cref="String"/> to its JSON string representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="delimiter">The string delimiter character.</param>
        /// <param name="stringEscapeHandling">The string escape handling.</param>
        /// <returns>A JSON string representation of the <see cref="String"/>.</returns>
        public static string ToString(string? value, char delimiter, StringEscapeHandling stringEscapeHandling)
        {
            if (delimiter != '"' && delimiter != '\'')
            {
                throw new ArgumentException("Delimiter must be a single or double quote.", nameof(delimiter));
            }

            return JavaScriptUtils.ToEscapedJavaScriptString(value, delimiter, true, stringEscapeHandling);
        }

        /// <summary>
        /// Converts the <see cref="Object"/> to its JSON string representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>A JSON string representation of the <see cref="Object"/>.</returns>
        public static string ToString(object? value)
        {
            if (value == null)
            {
                return Null;
            }

            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value.GetType());

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Pass only '\"' (double quote) or '\'' (single quote) as the delimiter.
  2. If the delimiter comes from configuration, validate it is one of the two allowed chars before calling ToString.
  3. Use the overload JsonConvert.ToString(string) which defaults to double quotes and avoids the parameter entirely.

Example fix

// before: passing a config-derived char that is not a quote
string json = JsonConvert.ToString(text, config.QuoteChar); // config.QuoteChar == '`'

// after: validate and fall back to a valid delimiter
char delim = (config.QuoteChar == '\'' || config.QuoteChar == '\"')
    ? config.QuoteChar
    : '\"';
string json = JsonConvert.ToString(text, delim);
Defensive patterns

Strategy: validation

Validate before calling

char delimiter = GetDelimiter();
if (delimiter != '\"' && delimiter != '\'')
    throw new ArgumentException($"Delimiter must be '\"' or '\'', got '{delimiter}'", nameof(delimiter));
string json = JsonConvert.ToString(text, delimiter);

Type guard

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

Try / catch

try { return JsonConvert.ToString(text, delimiter); }
catch (ArgumentException ex) when (ex.Message.Contains("single or double quote"))
{
    return JsonConvert.ToString(text, '\"'); // fall back to the JSON-standard double quote
}

Prevention

When it happens

Trigger: Calling the overload JsonConvert.ToString(value, delimiter) or ToString(value, delimiter, stringEscapeHandling) with a delimiter such as a backtick, pipe, or space. Typically from custom code that programmatically picks the quote character, or from passing the wrong variable (e.g. an integer code instead of a quote char).

Common situations: Helper utilities that parameterize the quote style and receive a typo'd or zero-value char. Casting an enum/int to char instead of passing the literal quote. Copy-paste errors where a config value meant to be '\"' is passed as something else.

Related errors


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