escapeJSONString

fnstring escapeJSONString(const string s, const char quote = '"', const bool asciiOnly = false) @safe

Escape a UTF-8 string for inclusion in a JSON/JSON5 string literal.

This function returns the escaped string without surrounding quotes.

Parameters

sInput UTF-8 string.
quoteQuote character that must be escaped (`'"'` or `'\''`).
asciiOnlyWhen true, escape non-ASCII code points using \uXXXX or surrogate pairs.

Returns

The escaped string. When no escaping is required and asciiOnly == false, returns s.