提交 717de500 编写于 作者: E Erick Tryzelaar

serialize: escaping json strings should write in batches.

This significantly speeds up encoding json strings.
上级 83f9f07e
...@@ -256,21 +256,37 @@ fn io_error_to_error(io: io::IoError) -> ParserError { ...@@ -256,21 +256,37 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
pub type EncodeResult = io::IoResult<()>; pub type EncodeResult = io::IoResult<()>;
pub type DecodeResult<T> = Result<T, DecoderError>; pub type DecodeResult<T> = Result<T, DecoderError>;
fn escape_bytes(writer: &mut io::Writer, s: &[u8]) -> Result<(), io::IoError> { pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError> {
try!(writer.write_str("\"")); try!(wr.write_str("\""));
for byte in s.iter() {
match *byte { let mut start = 0;
b'"' => try!(writer.write_str("\\\"")),
b'\\' => try!(writer.write_str("\\\\")), for (i, byte) in bytes.iter().enumerate() {
b'\x08' => try!(writer.write_str("\\b")), let escaped = match *byte {
b'\x0c' => try!(writer.write_str("\\f")), b'"' => "\\\"",
b'\n' => try!(writer.write_str("\\n")), b'\\' => "\\\\",
b'\r' => try!(writer.write_str("\\r")), b'\x08' => "\\b",
b'\t' => try!(writer.write_str("\\t")), b'\x0c' => "\\f",
_ => try!(writer.write_u8(*byte)), b'\n' => "\\n",
} b'\r' => "\\r",
} b'\t' => "\\t",
writer.write_str("\"") _ => { continue; }
};
if start < i {
try!(wr.write(bytes.slice(start, i)));
}
try!(wr.write_str(escaped));
start = i + 1;
}
if start != bytes.len() {
try!(wr.write(bytes.slice_from(start)));
}
wr.write_str("\"")
} }
fn escape_str(writer: &mut io::Writer, v: &str) -> Result<(), io::IoError> { fn escape_str(writer: &mut io::Writer, v: &str) -> Result<(), io::IoError> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册