Function nop_json::escape[][src]

pub fn escape(s: &str) -> Cow<'_, str>
Expand description

Adds slashes before " and \ characters, converts \t, \r, \n, \b, \f characters as needed, and encodes characters with codes less than space (32) with \u00XX. If input string doesn’t contain something that JSON standard wants us to escape, it just returns the input string without memory allocation.

Examples

let orig = "Some \"quote\" and some \\.";
let json_encoded = format!("{{\"value\": \"{}\"}}", nop_json::escape(orig));
assert_eq!(json_encoded, "{\"value\": \"Some \\\"quote\\\" and some \\\\.\"}");