pub fn write_string(out: &mut String, text: &str)Expand description
Appends text to out as a JSON string literal, quotes included.
The escaping is complete and the same every time:
"and\are escaped as\"and\\;- the control characters U+0000 to U+001F are escaped, with the short forms
\b \f \n \r \twhere JSON has one and\u00XX(lowercase hex) otherwise; - U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are escaped as
\u2028and\u2029: JSON allows them raw, but JavaScript before ES2019 ends a line at them, so raw ones break JSON embedded in a script; - everything else, including
/, DEL, and all other non-ASCII text, is copied unchanged as UTF-8.
This escaping is not HTML-safe: <, >, and & are copied, so
</script> inside a string stays </script> in the output. For JSON
placed inside HTML, write with Format::html_safe (and
Format::ascii for a channel that is not 8-bit clean).
So no string can end the literal early or put a raw line break in it,
whatever it contains: the output is always exactly one JSON string token
that decodes back to text. A string with nothing to escape (most keys
and values) is tested a word at a time and copied whole; otherwise the
unescaped stretches are copied in bulk, found eight bytes at a time.
ยงExamples
let mut out = String::from("{\"msg\":");
jsonx_lib::write_string(&mut out, "say \"hi\"\n\u{2028}");
out.push('}');
assert_eq!(out, r#"{"msg":"say \"hi\"\n\u2028"}"#);