{"record":{"id":"ad563c4a5af03bc8","repo":"JamesNK/Newtonsoft.Json","slug":"delimiter-must-be-a-single-or-double-quote","errorCode":null,"errorMessage":"Delimiter must be a single or double quote.","messagePattern":"Delimiter must be a single or double quote\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonConvert.cs","lineNumber":446,"sourceCode":"        /// <param name=\"delimiter\">The string delimiter character.</param>\n        /// <returns>A JSON string representation of the <see cref=\"String\"/>.</returns>\n        public static string ToString(string? value, char delimiter)\n        {\n            return ToString(value, delimiter, StringEscapeHandling.Default);\n        }\n\n        /// <summary>\n        /// Converts the <see cref=\"String\"/> to its JSON string representation.\n        /// </summary>\n        /// <param name=\"value\">The value to convert.</param>\n        /// <param name=\"delimiter\">The string delimiter character.</param>\n        /// <param name=\"stringEscapeHandling\">The string escape handling.</param>\n        /// <returns>A JSON string representation of the <see cref=\"String\"/>.</returns>\n        public static string ToString(string? value, char delimiter, StringEscapeHandling stringEscapeHandling)\n        {\n            if (delimiter != '\"' && delimiter != '\\'')\n            {\n                throw new ArgumentException(\"Delimiter must be a single or double quote.\", nameof(delimiter));\n            }\n\n            return JavaScriptUtils.ToEscapedJavaScriptString(value, delimiter, true, stringEscapeHandling);\n        }\n\n        /// <summary>\n        /// Converts the <see cref=\"Object\"/> to its JSON string representation.\n        /// </summary>\n        /// <param name=\"value\">The value to convert.</param>\n        /// <returns>A JSON string representation of the <see cref=\"Object\"/>.</returns>\n        public static string ToString(object? value)\n        {\n            if (value == null)\n            {\n                return Null;\n            }\n\n            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value.GetType());","sourceCodeStart":428,"sourceCodeEnd":464,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonConvert.cs#L428-L464","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass only '\\\"' (double quote) or '\\'' (single quote) as the delimiter.","If the delimiter comes from configuration, validate it is one of the two allowed chars before calling ToString.","Use the overload JsonConvert.ToString(string) which defaults to double quotes and avoids the parameter entirely."],"exampleFix":"// before: passing a config-derived char that is not a quote\nstring json = JsonConvert.ToString(text, config.QuoteChar); // config.QuoteChar == '`'\n\n// after: validate and fall back to a valid delimiter\nchar delim = (config.QuoteChar == '\\'' || config.QuoteChar == '\\\"')\n    ? config.QuoteChar\n    : '\\\"';\nstring json = JsonConvert.ToString(text, delim);","handlingStrategy":"validation","validationCode":"char delimiter = GetDelimiter();\nif (delimiter != '\\\"' && delimiter != '\\'')\n    throw new ArgumentException($\"Delimiter must be '\\\"' or '\\'', got '{delimiter}'\", nameof(delimiter));\nstring json = JsonConvert.ToString(text, delimiter);","typeGuard":"static bool IsValidQuoteChar(char c) => c == '\\\"' || c == '\\'';","tryCatchPattern":"try { return JsonConvert.ToString(text, delimiter); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"single or double quote\"))\n{\n    return JsonConvert.ToString(text, '\\\"'); // fall back to the JSON-standard double quote\n}","preventionTips":["Default to the overload JsonConvert.ToString(string) which uses double quotes.","Validate config-driven quote characters against the two allowed values.","Avoid casting ints/enums to char for the delimiter parameter."],"tags":["serialization","jsonconvert","argument","validation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}