Pike v8.1 release 6

Method Standards.JSON.escape_string()


Method escape_string

string escape_string(string str, void|int flags)

Description

Escapes string data for use in a JSON string.

8-bit and wider characters in input strings are neither escaped nor utf-8 encoded by default. string_to_utf8 can be used safely on the returned string to get a valid transport encoded JSON string.

The characters U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) are exceptions - they are encoded with \u escapes for compatibility. The reason is that those characters are not allowed in Javascript strings, so JSON parsers built in Javascript may have trouble with them otherwise.

Parameter val

The string to escape.

Parameter flags

Flag bit field to control formatting. ASCII_ONLY is the only flag that has any effect for this function.

Note

The difference between using this function and encode on a string is that encode returns quotations marks around the result.

See also

encode