Escape control characters in JSON output.
This commit is contained in:
parent
34d6800092
commit
fc30518be9
1 changed files with 12 additions and 8 deletions
|
@ -331,13 +331,14 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError
|
||||||
|
|
||||||
for (i, byte) in bytes.iter().enumerate() {
|
for (i, byte) in bytes.iter().enumerate() {
|
||||||
let escaped = match *byte {
|
let escaped = match *byte {
|
||||||
b'"' => "\\\"",
|
b'"' => "\\\"".into_cow(),
|
||||||
b'\\' => "\\\\",
|
b'\\' => "\\\\".into_cow(),
|
||||||
b'\x08' => "\\b",
|
b'\x08' => "\\b".into_cow(),
|
||||||
b'\x0c' => "\\f",
|
b'\x0c' => "\\f".into_cow(),
|
||||||
b'\n' => "\\n",
|
b'\n' => "\\n".into_cow(),
|
||||||
b'\r' => "\\r",
|
b'\r' => "\\r".into_cow(),
|
||||||
b'\t' => "\\t",
|
b'\t' => "\\t".into_cow(),
|
||||||
|
b'\x00'...b'\x1f' | b'\x7f' => format!("\\u00{:0>2x}", *byte).into_cow(),
|
||||||
_ => { continue; }
|
_ => { continue; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -345,7 +346,7 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError
|
||||||
try!(wr.write(bytes[start..i]));
|
try!(wr.write(bytes[start..i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
try!(wr.write_str(escaped));
|
try!(wr.write_str(escaped.deref()));
|
||||||
|
|
||||||
start = i + 1;
|
start = i + 1;
|
||||||
}
|
}
|
||||||
|
@ -2731,6 +2732,9 @@ mod tests {
|
||||||
fn test_write_char() {
|
fn test_write_char() {
|
||||||
check_encoder_for_simple!('a', "\"a\"");
|
check_encoder_for_simple!('a', "\"a\"");
|
||||||
check_encoder_for_simple!('\t', "\"\\t\"");
|
check_encoder_for_simple!('\t', "\"\\t\"");
|
||||||
|
check_encoder_for_simple!('\u{0000}', "\"\\u0000\"");
|
||||||
|
check_encoder_for_simple!('\u{001b}', "\"\\u001b\"");
|
||||||
|
check_encoder_for_simple!('\u{007f}', "\"\\u007f\"");
|
||||||
check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\"");
|
check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\"");
|
||||||
check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\"");
|
check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\"");
|
||||||
check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\"");
|
check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\"");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue