(s: &str, ascii_only: bool, w: &mut W)
| 73 | } |
| 74 | |
| 75 | pub fn write_json_string<W: io::Write>(s: &str, ascii_only: bool, w: &mut W) -> io::Result<()> { |
| 76 | w.write_all(b"\"")?; |
| 77 | let mut write_start_idx = 0; |
| 78 | let bytes = s.as_bytes(); |
| 79 | if ascii_only { |
| 80 | for (idx, c) in s.char_indices() { |
| 81 | if c.is_ascii() { |
| 82 | if let Some(escaped) = json_escaped_char(c as u8) { |
| 83 | w.write_all(&bytes[write_start_idx..idx])?; |
| 84 | w.write_all(escaped.as_bytes())?; |
| 85 | write_start_idx = idx + 1; |
| 86 | } |
| 87 | } else { |
| 88 | w.write_all(&bytes[write_start_idx..idx])?; |
| 89 | write_start_idx = idx + c.len_utf8(); |
| 90 | // codepoints outside the BMP get 2 '\uxxxx' sequences to represent them |
| 91 | for point in c.encode_utf16(&mut [0; 2]) { |
| 92 | write!(w, "\\u{point:04x}")?; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } else { |
| 97 | for (idx, c) in s.bytes().enumerate() { |
| 98 | if let Some(escaped) = json_escaped_char(c) { |
| 99 | w.write_all(&bytes[write_start_idx..idx])?; |
| 100 | w.write_all(escaped.as_bytes())?; |
| 101 | write_start_idx = idx + 1; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | w.write_all(&bytes[write_start_idx..])?; |
| 106 | w.write_all(b"\"") |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug)] |
| 110 | pub struct DecodeError { |
no test coverage detected