Skip to content

Commit

Permalink
serialize: escaping json strings should write in batches.
Browse files Browse the repository at this point in the history
This significantly speeds up encoding json strings.
  • Loading branch information
erickt committed Jul 4, 2014
1 parent 83f9f07 commit 717de50
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/libserialize/json.rs
Expand Up @@ -256,21 +256,37 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
pub type EncodeResult = io::IoResult<()>;
pub type DecodeResult<T> = Result<T, DecoderError>;

fn escape_bytes(writer: &mut io::Writer, s: &[u8]) -> Result<(), io::IoError> {
try!(writer.write_str("\""));
for byte in s.iter() {
match *byte {
b'"' => try!(writer.write_str("\\\"")),
b'\\' => try!(writer.write_str("\\\\")),
b'\x08' => try!(writer.write_str("\\b")),
b'\x0c' => try!(writer.write_str("\\f")),
b'\n' => try!(writer.write_str("\\n")),
b'\r' => try!(writer.write_str("\\r")),
b'\t' => try!(writer.write_str("\\t")),
_ => try!(writer.write_u8(*byte)),
}
}
writer.write_str("\"")
pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError> {
try!(wr.write_str("\""));

let mut start = 0;

for (i, byte) in bytes.iter().enumerate() {
let escaped = match *byte {
b'"' => "\\\"",
b'\\' => "\\\\",
b'\x08' => "\\b",
b'\x0c' => "\\f",
b'\n' => "\\n",
b'\r' => "\\r",
b'\t' => "\\t",
_ => { 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> {
Expand Down

0 comments on commit 717de50

Please sign in to comment.