A JSON value kept as the exact bytes that will be sent. The API
accepts a string, an object or an array wherever it takes free-form
content (state, instructions, descriptions).
14impl Json {
A JSON string holding text.
json as written, after checking it is JSON.
json in RFC 8785 canonical form: sorted keys, no whitespace,
ECMAScript number formatting, and numbers a double cannot hold
exactly kept as strings. Equal values give equal bytes.
35 pub fn as_str(&self) -> &str { 36 self.0.get() 37 } 38} 39 40impl fmt::Debug for Json { 41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 42 f.write_str(self.as_str()) 43 } 44} 45 46impl PartialEq for Json { 47 fn eq(&self, other: &Self) -> bool { 48 self.as_str() == other.as_str() 49 } 50} 51 52impl Serialize for Json { 53 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { 54 self.0.serialize(s) 55 } 56} 57 58#[cfg(test)] 59mod tests { 60 use super::*; 61 62 #[test] 63 fn canonical_sorts_keys_and_formats_numbers() { 64 let j = Json::canonical(r#"{ "id": 1, "body": "hi", "a": 1.0e0, "big": 1e21, "bigint": 9007199254740993 }"#).unwrap(); 65 assert_eq!(j.as_str(), r#"{"a":1,"big":1e+21,"bigint":"9007199254740993","body":"hi","id":1}"#); 66 } 67 68 #[test] 69 fn text_is_a_json_string() { 70 assert_eq!(Json::text("say \"hi\"").as_str(), r#""say \"hi\"""#); 71 } 72 73 #[test] 74 fn verbatim_keeps_bytes() { 75 assert_eq!(Json::verbatim(r#"{"b":1, "a":2}"#).unwrap().as_str(), r#"{"b":1, "a":2}"#); 76 assert!(Json::verbatim("{").is_err()); 77 } 78}