pub fn from_str_raw<'de, D>(s: &'de str) -> Result<D>
where D: Deserialize<'de>,
Expand description

Deserialize a JSON with comments text as type D. Deserialized instance may have raw string value that contain escape sequence. This function can deserialize any string value as borrowed &str.

§Examples

let target = r#"    "\"q\" \\s\/ l\n"    "#;
let no_escaped: &str = json_with_comments::from_str_raw(target).unwrap();
assert_eq!(no_escaped, r#"\"q\" \\s\/ l\n"#);

let unescaped: String = json_with_comments::from_str(target).unwrap();
assert_eq!(unescaped, "\"q\" \\s/ l\n");

§Notes

This function can deserialize any string as borrowed &str. But, if it contain escape sequence such as "\n", it be deserialized as it is. If you need to deserialize string value as unescaped owned String, use from_str.

use std::borrow::Cow;
use json_with_comments::from_str_raw;

let no_escaped = r#"  "string without linefeed"  "#;
assert_eq!(from_str_raw::<String>(no_escaped).unwrap(), "string without linefeed");
assert_eq!(from_str_raw::<Cow<'_, str>>(no_escaped).unwrap(), "string without linefeed");
assert_eq!(from_str_raw::<&str>(no_escaped).unwrap(), "string without linefeed");

let escaped = r#"  "string with linefeed\n"  "#;
assert_eq!(from_str_raw::<String>(escaped).unwrap(), "string with linefeed\\n");
assert_eq!(from_str_raw::<Cow<'_, str>>(escaped).unwrap(), "string with linefeed\\n");
assert_eq!(from_str_raw::<&str>(escaped).unwrap(), "string with linefeed\\n"); // deserialized as it is