ported over to the new std::io, but it's half the speed of std::old_io
This commit is contained in:
parent
4dccf8cdec
commit
004a89510e
193
benches/bench_log.rs
Executable file → Normal file
193
benches/bench_log.rs
Executable file → Normal file
@ -6,9 +6,7 @@ extern crate serde;
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
extern crate test;
|
||||
|
||||
use std::old_io::ByRefWriter;
|
||||
use std::old_io::extensions::Bytes;
|
||||
use std::old_io;
|
||||
use std::io::{self, ReadExt, WriteExt};
|
||||
use std::num::FromPrimitive;
|
||||
use test::Bencher;
|
||||
|
||||
@ -616,10 +614,15 @@ impl MyMemWriter0 {
|
||||
}
|
||||
|
||||
|
||||
impl Writer for MyMemWriter0 {
|
||||
impl io::Write for MyMemWriter0 {
|
||||
#[inline]
|
||||
fn write_all(&mut self, buf: &[u8]) -> old_io::IoResult<()> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.buf.push_all(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -656,10 +659,15 @@ fn push_all_bytes(dst: &mut Vec<u8>, src: &[u8]) {
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer for MyMemWriter1 {
|
||||
impl io::Write for MyMemWriter1 {
|
||||
#[inline]
|
||||
fn write_all(&mut self, buf: &[u8]) -> old_io::IoResult<()> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
push_all_bytes(&mut self.buf, buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -759,7 +767,7 @@ fn bench_serializer_slice(b: &mut Bencher) {
|
||||
|
||||
b.iter(|| {
|
||||
for item in buf.iter_mut(){ *item = 0; }
|
||||
let mut wr = std::old_io::BufWriter::new(&mut buf);
|
||||
let mut wr = &mut buf[];
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
log.serialize(&mut serializer).unwrap();
|
||||
@ -839,166 +847,166 @@ fn bench_copy(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
fn manual_serialize_no_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str("{\"timestamp\":").unwrap();
|
||||
fn manual_serialize_no_escape<W: io::Write>(wr: &mut W, log: &Log) {
|
||||
(write!(wr, "{{\"timestamp\":")).unwrap();
|
||||
(write!(wr, "{}", log.timestamp)).unwrap();
|
||||
wr.write_str(",\"zone_id\":").unwrap();
|
||||
(write!(wr, ",\"zone_id\":")).unwrap();
|
||||
(write!(wr, "{}", log.zone_id)).unwrap();
|
||||
wr.write_str(",\"zone_plan\":").unwrap();
|
||||
(write!(wr, ",\"zone_plan\":")).unwrap();
|
||||
(write!(wr, "{}", log.zone_plan as usize)).unwrap();
|
||||
|
||||
wr.write_str(",\"http\":{\"protocol\":").unwrap();
|
||||
(write!(wr, ",\"http\":{{\"protocol\":")).unwrap();
|
||||
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
|
||||
wr.write_str(",\"status\":").unwrap();
|
||||
(write!(wr, ",\"status\":")).unwrap();
|
||||
(write!(wr, "{}", log.http.status)).unwrap();
|
||||
wr.write_str(",\"host_status\":").unwrap();
|
||||
(write!(wr, ",\"host_status\":")).unwrap();
|
||||
(write!(wr, "{}", log.http.host_status)).unwrap();
|
||||
wr.write_str(",\"up_status\":").unwrap();
|
||||
(write!(wr, ",\"up_status\":")).unwrap();
|
||||
(write!(wr, "{}", log.http.up_status)).unwrap();
|
||||
wr.write_str(",\"method\":").unwrap();
|
||||
(write!(wr, ",\"method\":")).unwrap();
|
||||
(write!(wr, "{}", log.http.method as usize)).unwrap();
|
||||
wr.write_str(",\"content_type\":").unwrap();
|
||||
(write!(wr, ",\"content_type\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.content_type)).unwrap();
|
||||
wr.write_str(",\"user_agent\":").unwrap();
|
||||
(write!(wr, ",\"user_agent\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.user_agent)).unwrap();
|
||||
wr.write_str(",\"referer\":").unwrap();
|
||||
(write!(wr, ",\"referer\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.referer)).unwrap();
|
||||
wr.write_str(",\"request_uri\":").unwrap();
|
||||
(write!(wr, ",\"request_uri\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.http.request_uri)).unwrap();
|
||||
|
||||
wr.write_str("},\"origin\":{").unwrap();
|
||||
(write!(wr, "}},\"origin\":{{")).unwrap();
|
||||
|
||||
wr.write_str("\"ip\":").unwrap();
|
||||
(write!(wr, "\"ip\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.origin.ip)).unwrap();
|
||||
wr.write_str(",\"port\":").unwrap();
|
||||
(write!(wr, ",\"port\":")).unwrap();
|
||||
(write!(wr, "{}", log.origin.port)).unwrap();
|
||||
wr.write_str(",\"hostname\":").unwrap();
|
||||
(write!(wr, ",\"hostname\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.origin.hostname)).unwrap();
|
||||
|
||||
wr.write_str(",\"protocol\":").unwrap();
|
||||
(write!(wr, ",\"protocol\":")).unwrap();
|
||||
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
|
||||
|
||||
wr.write_str("},\"country\":").unwrap();
|
||||
(write!(wr, "}},\"country\":")).unwrap();
|
||||
(write!(wr, "{}", log.country as usize)).unwrap();
|
||||
wr.write_str(",\"cache_status\":").unwrap();
|
||||
(write!(wr, ",\"cache_status\":")).unwrap();
|
||||
(write!(wr, "{}", log.cache_status as usize)).unwrap();
|
||||
wr.write_str(",\"server_ip\":").unwrap();
|
||||
(write!(wr, ",\"server_ip\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.server_ip)).unwrap();
|
||||
wr.write_str(",\"server_name\":").unwrap();
|
||||
(write!(wr, ",\"server_name\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.server_name)).unwrap();
|
||||
wr.write_str(",\"remote_ip\":").unwrap();
|
||||
(write!(wr, ",\"remote_ip\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.remote_ip)).unwrap();
|
||||
wr.write_str(",\"bytes_dlv\":").unwrap();
|
||||
(write!(wr, ",\"bytes_dlv\":")).unwrap();
|
||||
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
||||
|
||||
wr.write_str(",\"ray_id\":").unwrap();
|
||||
(write!(wr, ",\"ray_id\":")).unwrap();
|
||||
(write!(wr, "\"{}\"", log.ray_id)).unwrap();
|
||||
wr.write_str("}").unwrap();
|
||||
(write!(wr, "}}")).unwrap();
|
||||
}
|
||||
|
||||
fn manual_serialize_escape<W: Writer>(wr: &mut W, log: &Log) {
|
||||
wr.write_str("{").unwrap();
|
||||
fn manual_serialize_escape<W: io::Write>(wr: &mut W, log: &Log) {
|
||||
(write!(wr, "{{")).unwrap();
|
||||
escape_str(wr, "timestamp").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.timestamp)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "zone_id").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.zone_id)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "zone_plan").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.zone_plan as isize)).unwrap();
|
||||
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "http").unwrap();
|
||||
wr.write_str(":{").unwrap();
|
||||
(write!(wr, ":{{")).unwrap();
|
||||
escape_str(wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.http.protocol as usize)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.http.status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "host_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.http.host_status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "up_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.http.up_status)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "method").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.http.method as usize)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "content_type").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.http.content_type).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "user_agent").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.http.user_agent).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "referer").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.http.referer).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "request_uri").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.http.request_uri).unwrap();
|
||||
|
||||
wr.write_str("},").unwrap();
|
||||
(write!(wr, "}},")).unwrap();
|
||||
escape_str(wr, "origin").unwrap();
|
||||
wr.write_str(":{").unwrap();
|
||||
(write!(wr, ":{{")).unwrap();
|
||||
|
||||
escape_str(wr, "ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.origin.ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "port").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.origin.port)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "hostname").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.origin.hostname).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "protocol").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.origin.protocol as usize)).unwrap();
|
||||
|
||||
wr.write_str("},").unwrap();
|
||||
(write!(wr, "}},")).unwrap();
|
||||
escape_str(wr, "country").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.country as usize)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "cache_status").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.cache_status as usize)).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "server_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.server_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "server_name").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.server_name).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "remote_ip").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.remote_ip).unwrap();
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "bytes_dlv").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
(write!(wr, "{}", log.bytes_dlv)).unwrap();
|
||||
|
||||
wr.write_str(",").unwrap();
|
||||
(write!(wr, ",")).unwrap();
|
||||
escape_str(wr, "ray_id").unwrap();
|
||||
wr.write_str(":").unwrap();
|
||||
(write!(wr, ":")).unwrap();
|
||||
escape_str(wr, &log.ray_id).unwrap();
|
||||
wr.write_str("}").unwrap();
|
||||
(write!(wr, "}}")).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1156,7 +1164,7 @@ fn bench_manual_serialize_my_mem_writer1_escape(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
fn direct<W: Writer>(wr: &mut W, log: &Log) {
|
||||
fn direct<W: io::Write>(wr: &mut W, log: &Log) {
|
||||
use serde::ser::Serializer;
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
@ -1653,9 +1661,8 @@ fn bench_iter_manual_iter_deserializers(b: &mut Bencher) {
|
||||
|
||||
#[test]
|
||||
fn test_iter_manual_reader_as_iter_deserializer() {
|
||||
let mut rdr = JSON_STR.as_bytes();
|
||||
let iter = Bytes::new(&mut rdr)
|
||||
.map(|x| x.unwrap());
|
||||
let rdr = JSON_STR.as_bytes();
|
||||
let iter = rdr.bytes().map(|x| x.unwrap());
|
||||
|
||||
let log = manual_iter_deserialize(iter);
|
||||
|
||||
@ -1667,9 +1674,8 @@ fn bench_iter_manual_reader_as_iter_deserializer(b: &mut Bencher) {
|
||||
b.bytes = JSON_STR.len() as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut rdr = JSON_STR.as_bytes();
|
||||
let iter = Bytes::new(&mut rdr)
|
||||
.map(|x| x.unwrap());
|
||||
let rdr = JSON_STR.as_bytes();
|
||||
let iter = rdr.bytes().map(|x| x.unwrap());
|
||||
|
||||
let _ = manual_iter_deserialize(iter);
|
||||
});
|
||||
@ -1680,9 +1686,8 @@ fn bench_iter_manual_reader_as_iter_deserializers(b: &mut Bencher) {
|
||||
b.bytes = JSON_STR.len() as u64;
|
||||
|
||||
for _ in range(0is, 10000) {
|
||||
let mut rdr = JSON_STR.as_bytes();
|
||||
let iter = Bytes::new(&mut rdr)
|
||||
.map(|x| x.unwrap());
|
||||
let rdr = JSON_STR.as_bytes();
|
||||
let iter = rdr.bytes().map(|x| x.unwrap());
|
||||
|
||||
let _ = manual_iter_deserialize(iter);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::old_io;
|
||||
use std::io;
|
||||
|
||||
use de::{Token, TokenKind};
|
||||
|
||||
@ -82,7 +82,7 @@ impl fmt::Debug for ErrorCode {
|
||||
pub enum Error {
|
||||
/// msg, line, col
|
||||
SyntaxError(ErrorCode, usize, usize),
|
||||
IoError(old_io::IoError),
|
||||
IoError(io::Error),
|
||||
ExpectedError(String, String),
|
||||
MissingFieldError(String),
|
||||
UnknownVariantError(String),
|
||||
@ -120,8 +120,8 @@ impl fmt::Display for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl error::FromError<old_io::IoError> for Error {
|
||||
fn from_error(error: old_io::IoError) -> Error {
|
||||
impl error::FromError<io::Error> for Error {
|
||||
fn from_error(error: io::Error) -> Error {
|
||||
Error::IoError(error)
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ To serialize using `Serialize`:
|
||||
|
||||
extern crate serde;
|
||||
|
||||
use std::old_io::ByRefWriter;
|
||||
use std::io::WriteExt;
|
||||
use serde::json;
|
||||
use serde::Serialize;
|
||||
|
||||
|
316
src/json/ser.rs
316
src/json/ser.rs
@ -1,14 +1,14 @@
|
||||
use std::f32;
|
||||
use std::f64;
|
||||
use std::num::{Float, FpCategory};
|
||||
use std::old_io::{IoError, IoResult};
|
||||
use std::io;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use ser::Serialize;
|
||||
use ser;
|
||||
|
||||
fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
||||
try!(wr.write_str("\""));
|
||||
fn escape_bytes<W: io::Write>(wr: &mut W, bytes: &[u8]) -> io::Result<()> {
|
||||
try!(write!(wr, "\""));
|
||||
|
||||
let mut start = 0;
|
||||
|
||||
@ -28,7 +28,7 @@ fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
||||
try!(wr.write_all(&bytes[start..i]));
|
||||
}
|
||||
|
||||
try!(wr.write_str(escaped));
|
||||
try!(write!(wr, "{}", escaped));
|
||||
|
||||
start = i + 1;
|
||||
}
|
||||
@ -37,34 +37,34 @@ fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> IoResult<()> {
|
||||
try!(wr.write_all(&bytes[start..]));
|
||||
}
|
||||
|
||||
wr.write_str("\"")
|
||||
write!(wr, "\"")
|
||||
}
|
||||
|
||||
pub fn escape_str<W: Writer>(wr: &mut W, v: &str) -> IoResult<()> {
|
||||
pub fn escape_str<W: io::Write>(wr: &mut W, v: &str) -> io::Result<()> {
|
||||
escape_bytes(wr, v.as_bytes())
|
||||
}
|
||||
|
||||
fn escape_char<W: Writer>(wr: &mut W, v: char) -> IoResult<()> {
|
||||
fn escape_char<W: io::Write>(wr: &mut W, v: char) -> io::Result<()> {
|
||||
let buf = &mut [0; 4];
|
||||
v.encode_utf8(buf);
|
||||
escape_bytes(wr, buf)
|
||||
}
|
||||
|
||||
fn fmt_f32_or_null<W: Writer>(wr: &mut W, v: f32) -> IoResult<()> {
|
||||
fn fmt_f32_or_null<W: io::Write>(wr: &mut W, v: f32) -> io::Result<()> {
|
||||
match v.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_str("null"),
|
||||
_ => wr.write_str(&f32::to_str_digits(v, 6)),
|
||||
FpCategory::Nan | FpCategory::Infinite => write!(wr, "null"),
|
||||
_ => write!(wr, "{}", f32::to_str_digits(v, 6)),
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_f64_or_null<W: Writer>(wr: &mut W, v: f64) -> IoResult<()> {
|
||||
fn fmt_f64_or_null<W: io::Write>(wr: &mut W, v: f64) -> io::Result<()> {
|
||||
match v.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => wr.write_str("null"),
|
||||
_ => wr.write_str(&f64::to_str_digits(v, 6)),
|
||||
FpCategory::Nan | FpCategory::Infinite => write!(wr, "null"),
|
||||
_ => write!(wr, "{}", f64::to_str_digits(v, 6)),
|
||||
}
|
||||
}
|
||||
|
||||
fn spaces<W: Writer>(wr: &mut W, mut n: usize) -> IoResult<()> {
|
||||
fn spaces<W: io::Write>(wr: &mut W, mut n: usize) -> io::Result<()> {
|
||||
const LEN: usize = 16;
|
||||
const BUF: &'static [u8; LEN] = &[b' '; LEN];
|
||||
|
||||
@ -96,7 +96,7 @@ pub struct Serializer<W> {
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<W: Writer> Serializer<W> {
|
||||
impl<W: io::Write> Serializer<W> {
|
||||
/// Creates a new JSON serializer whose output will be written to the writer
|
||||
/// specified.
|
||||
pub fn new(wr: W) -> Serializer<W> {
|
||||
@ -106,174 +106,174 @@ impl<W: Writer> Serializer<W> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the Writer from the Serializer.
|
||||
/// Unwrap the io::Write from the Serializer.
|
||||
pub fn unwrap(self) -> W {
|
||||
self.wr
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Writer> ser::Serializer<IoError> for Serializer<W> {
|
||||
impl<W: io::Write> ser::Serializer<io::Error> for Serializer<W> {
|
||||
#[inline]
|
||||
fn serialize_null(&mut self) -> IoResult<()> {
|
||||
self.wr.write_str("null")
|
||||
fn serialize_null(&mut self) -> io::Result<()> {
|
||||
write!(&mut self.wr, "null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_bool(&mut self, v: bool) -> IoResult<()> {
|
||||
fn serialize_bool(&mut self, v: bool) -> io::Result<()> {
|
||||
if v {
|
||||
self.wr.write_str("true")
|
||||
write!(&mut self.wr, "true")
|
||||
} else {
|
||||
self.wr.write_str("false")
|
||||
write!(&mut self.wr, "false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_isize(&mut self, v: isize) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_isize(&mut self, v: isize) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i8(&mut self, v: i8) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i16(&mut self, v: i16) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i32(&mut self, v: i32) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i64(&mut self, v: i64) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_usize(&mut self, v: usize) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_usize(&mut self, v: usize) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u8(&mut self, v: u8) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u16(&mut self, v: u16) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u32(&mut self, v: u32) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u64(&mut self, v: u64) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> IoResult<()> {
|
||||
fn serialize_f32(&mut self, v: f32) -> io::Result<()> {
|
||||
fmt_f32_or_null(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f64(&mut self, v: f64) -> IoResult<()> {
|
||||
fn serialize_f64(&mut self, v: f64) -> io::Result<()> {
|
||||
fmt_f64_or_null(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_char(&mut self, v: char) -> IoResult<()> {
|
||||
fn serialize_char(&mut self, v: char) -> io::Result<()> {
|
||||
escape_char(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, v: &str) -> IoResult<()> {
|
||||
fn serialize_str(&mut self, v: &str) -> io::Result<()> {
|
||||
escape_str(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_start(&mut self, _len: usize) -> IoResult<()> {
|
||||
fn serialize_tuple_start(&mut self, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
self.wr.write_str("[")
|
||||
write!(&mut self.wr, "[")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_elt<
|
||||
T: Serialize<Serializer<W>, IoError>
|
||||
>(&mut self, value: &T) -> IoResult<()> {
|
||||
T: Serialize<Serializer<W>, io::Error>
|
||||
>(&mut self, value: &T) -> io::Result<()> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
try!(self.wr.write_str(","));
|
||||
try!(write!(&mut self.wr, ","));
|
||||
}
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self) -> IoResult<()> {
|
||||
self.wr.write_str("]")
|
||||
fn serialize_tuple_end(&mut self) -> io::Result<()> {
|
||||
write!(&mut self.wr, "]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> IoResult<()> {
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
self.wr.write_str("{")
|
||||
write!(&mut self.wr, "{{")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_elt<
|
||||
T: Serialize<Serializer<W>, IoError>
|
||||
>(&mut self, name: &str, value: &T) -> IoResult<()> {
|
||||
T: Serialize<Serializer<W>, io::Error>
|
||||
>(&mut self, name: &str, value: &T) -> io::Result<()> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
try!(self.wr.write_str(","));
|
||||
try!(write!(&mut self.wr, ","));
|
||||
}
|
||||
try!(name.serialize(self));
|
||||
try!(self.wr.write_str(":"));
|
||||
try!(write!(&mut self.wr, ":"));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_end(&mut self) -> IoResult<()> {
|
||||
self.wr.write_str("}")
|
||||
fn serialize_struct_end(&mut self) -> io::Result<()> {
|
||||
write!(&mut self.wr, "}}")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: usize) -> IoResult<()> {
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
try!(self.wr.write_str("{"));
|
||||
try!(write!(&mut self.wr, "{{"));
|
||||
try!(self.serialize_str(variant));
|
||||
self.wr.write_str(":[")
|
||||
write!(&mut self.wr, ":[")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_elt<
|
||||
T: Serialize<Serializer<W>, IoError>
|
||||
>(&mut self, value: &T) -> IoResult<()> {
|
||||
T: Serialize<Serializer<W>, io::Error>
|
||||
>(&mut self, value: &T) -> io::Result<()> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
try!(self.wr.write_str(","));
|
||||
try!(write!(&mut self.wr, ","));
|
||||
}
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_end(&mut self) -> IoResult<()> {
|
||||
self.wr.write_str("]}")
|
||||
fn serialize_enum_end(&mut self) -> io::Result<()> {
|
||||
write!(&mut self.wr, "]}}")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_option<
|
||||
T: Serialize<Serializer<W>, IoError>
|
||||
>(&mut self, v: &Option<T>) -> IoResult<()> {
|
||||
T: Serialize<Serializer<W>, io::Error>
|
||||
>(&mut self, v: &Option<T>) -> io::Result<()> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
v.serialize(self)
|
||||
@ -286,42 +286,42 @@ impl<W: Writer> ser::Serializer<IoError> for Serializer<W> {
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq<
|
||||
T: Serialize<Serializer<W>, IoError>,
|
||||
T: Serialize<Serializer<W>, io::Error>,
|
||||
Iter: Iterator<Item=T>
|
||||
>(&mut self, iter: Iter) -> IoResult<()> {
|
||||
try!(self.wr.write_str("["));
|
||||
>(&mut self, iter: Iter) -> io::Result<()> {
|
||||
try!(write!(&mut self.wr, "["));
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(self.wr.write_str(","));
|
||||
try!(write!(&mut self.wr, ","));
|
||||
}
|
||||
try!(elt.serialize(self));
|
||||
|
||||
}
|
||||
self.wr.write_str("]")
|
||||
write!(&mut self.wr, "]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map<
|
||||
K: Serialize<Serializer<W>, IoError>,
|
||||
V: Serialize<Serializer<W>, IoError>,
|
||||
K: Serialize<Serializer<W>, io::Error>,
|
||||
V: Serialize<Serializer<W>, io::Error>,
|
||||
Iter: Iterator<Item=(K, V)>
|
||||
>(&mut self, iter: Iter) -> IoResult<()> {
|
||||
try!(self.wr.write_str("{"));
|
||||
>(&mut self, iter: Iter) -> io::Result<()> {
|
||||
try!(write!(&mut self.wr, "{{"));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(self.wr.write_str(","));
|
||||
try!(write!(&mut self.wr, ","));
|
||||
}
|
||||
try!(key.serialize(self));
|
||||
try!(self.wr.write_str(":"));
|
||||
try!(write!(&mut self.wr, ":"));
|
||||
try!(value.serialize(self));
|
||||
}
|
||||
self.wr.write_str("}")
|
||||
write!(&mut self.wr, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,7 +333,7 @@ pub struct PrettySerializer<W> {
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<W: Writer> PrettySerializer<W> {
|
||||
impl<W: io::Write> PrettySerializer<W> {
|
||||
/// Creates a new serializer whose output will be written to the specified writer
|
||||
pub fn new(wr: W) -> PrettySerializer<W> {
|
||||
PrettySerializer {
|
||||
@ -343,191 +343,191 @@ impl<W: Writer> PrettySerializer<W> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the Writer from the Serializer.
|
||||
/// Unwrap the io::Write from the Serializer.
|
||||
pub fn unwrap(self) -> W {
|
||||
self.wr
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_sep(&mut self) -> IoResult<()> {
|
||||
fn serialize_sep(&mut self) -> io::Result<()> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
self.indent += 2;
|
||||
try!(self.wr.write_str("\n"));
|
||||
try!(write!(&mut self.wr, "\n"));
|
||||
} else {
|
||||
try!(self.wr.write_str(",\n"));
|
||||
try!(write!(&mut self.wr, ",\n"));
|
||||
}
|
||||
|
||||
spaces(&mut self.wr, self.indent)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_end(&mut self, s: &str) -> IoResult<()> {
|
||||
fn serialize_end(&mut self, s: &str) -> io::Result<()> {
|
||||
if !self.first {
|
||||
try!(self.wr.write_str("\n"));
|
||||
try!(write!(&mut self.wr, "\n"));
|
||||
self.indent -= 2;
|
||||
try!(spaces(&mut self.wr, self.indent));
|
||||
}
|
||||
|
||||
self.first = false;
|
||||
|
||||
self.wr.write_str(s)
|
||||
write!(&mut self.wr, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
|
||||
impl<W: io::Write> ser::Serializer<io::Error> for PrettySerializer<W> {
|
||||
#[inline]
|
||||
fn serialize_null(&mut self) -> IoResult<()> {
|
||||
self.wr.write_str("null")
|
||||
fn serialize_null(&mut self) -> io::Result<()> {
|
||||
write!(&mut self.wr, "null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_bool(&mut self, v: bool) -> IoResult<()> {
|
||||
fn serialize_bool(&mut self, v: bool) -> io::Result<()> {
|
||||
if v {
|
||||
self.wr.write_str("true")
|
||||
write!(&mut self.wr, "true")
|
||||
} else {
|
||||
self.wr.write_str("false")
|
||||
write!(&mut self.wr, "false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_isize(&mut self, v: isize) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_isize(&mut self, v: isize) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i8(&mut self, v: i8) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i16(&mut self, v: i16) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i32(&mut self, v: i32) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_i64(&mut self, v: i64) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_usize(&mut self, v: usize) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_usize(&mut self, v: usize) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u8(&mut self, v: u8) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u16(&mut self, v: u16) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u32(&mut self, v: u32) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> IoResult<()> {
|
||||
write!(&mut self.wr, "{}", v)
|
||||
fn serialize_u64(&mut self, v: u64) -> io::Result<()> {
|
||||
write!(&mut &mut self.wr, "{}", v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> IoResult<()> {
|
||||
fn serialize_f32(&mut self, v: f32) -> io::Result<()> {
|
||||
fmt_f32_or_null(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f64(&mut self, v: f64) -> IoResult<()> {
|
||||
fn serialize_f64(&mut self, v: f64) -> io::Result<()> {
|
||||
fmt_f64_or_null(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_char(&mut self, v: char) -> IoResult<()> {
|
||||
fn serialize_char(&mut self, v: char) -> io::Result<()> {
|
||||
escape_char(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, v: &str) -> IoResult<()> {
|
||||
fn serialize_str(&mut self, v: &str) -> io::Result<()> {
|
||||
escape_str(&mut self.wr, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_start(&mut self, _len: usize) -> IoResult<()> {
|
||||
fn serialize_tuple_start(&mut self, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
self.wr.write_str("[")
|
||||
write!(&mut self.wr, "[")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_elt<
|
||||
T: Serialize<PrettySerializer<W>, IoError>
|
||||
>(&mut self, value: &T) -> IoResult<()> {
|
||||
T: Serialize<PrettySerializer<W>, io::Error>
|
||||
>(&mut self, value: &T) -> io::Result<()> {
|
||||
try!(self.serialize_sep());
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self) -> IoResult<()> {
|
||||
fn serialize_tuple_end(&mut self) -> io::Result<()> {
|
||||
self.serialize_end("]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> IoResult<()> {
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
self.wr.write_str("{")
|
||||
write!(&mut self.wr, "{{")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_elt<
|
||||
T: Serialize<PrettySerializer<W>, IoError>
|
||||
>(&mut self, name: &str, value: &T) -> IoResult<()> {
|
||||
T: Serialize<PrettySerializer<W>, io::Error>
|
||||
>(&mut self, name: &str, value: &T) -> io::Result<()> {
|
||||
try!(self.serialize_sep());
|
||||
try!(self.serialize_str(name));
|
||||
try!(self.wr.write_str(": "));
|
||||
try!(write!(&mut self.wr, ": "));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_end(&mut self) -> IoResult<()> {
|
||||
fn serialize_struct_end(&mut self) -> io::Result<()> {
|
||||
self.serialize_end("}")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: usize) -> IoResult<()> {
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: usize) -> io::Result<()> {
|
||||
self.first = true;
|
||||
try!(self.wr.write_str("{"));
|
||||
try!(write!(&mut self.wr, "{{"));
|
||||
try!(self.serialize_sep());
|
||||
try!(self.serialize_str(variant));
|
||||
self.first = true;
|
||||
self.wr.write_str(": [")
|
||||
write!(&mut self.wr, ": [")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_elt<
|
||||
T: Serialize<PrettySerializer<W>, IoError>
|
||||
>(&mut self, value: &T) -> IoResult<()> {
|
||||
T: Serialize<PrettySerializer<W>, io::Error>
|
||||
>(&mut self, value: &T) -> io::Result<()> {
|
||||
try!(self.serialize_sep());
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_end(&mut self) -> IoResult<()> {
|
||||
fn serialize_enum_end(&mut self) -> io::Result<()> {
|
||||
try!(self.serialize_tuple_end());
|
||||
self.serialize_struct_end()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_option<
|
||||
T: Serialize<PrettySerializer<W>, IoError>
|
||||
>(&mut self, v: &Option<T>) -> IoResult<()> {
|
||||
T: Serialize<PrettySerializer<W>, io::Error>
|
||||
>(&mut self, v: &Option<T>) -> io::Result<()> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
v.serialize(self)
|
||||
@ -540,10 +540,10 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq<
|
||||
T: Serialize<PrettySerializer<W>, IoError>,
|
||||
T: Serialize<PrettySerializer<W>, io::Error>,
|
||||
Iter: Iterator<Item=T>
|
||||
>(&mut self, iter: Iter) -> IoResult<()> {
|
||||
try!(self.wr.write_str("["));
|
||||
>(&mut self, iter: Iter) -> io::Result<()> {
|
||||
try!(write!(&mut self.wr, "["));
|
||||
|
||||
self.first = true;
|
||||
for elt in iter {
|
||||
@ -556,17 +556,17 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
|
||||
|
||||
#[inline]
|
||||
fn serialize_map<
|
||||
K: Serialize<PrettySerializer<W>, IoError>,
|
||||
V: Serialize<PrettySerializer<W>, IoError>,
|
||||
K: Serialize<PrettySerializer<W>, io::Error>,
|
||||
V: Serialize<PrettySerializer<W>, io::Error>,
|
||||
Iter: Iterator<Item=(K, V)>
|
||||
>(&mut self, iter: Iter) -> IoResult<()> {
|
||||
try!(self.wr.write_str("{"));
|
||||
>(&mut self, iter: Iter) -> io::Result<()> {
|
||||
try!(write!(&mut self.wr, "{{"));
|
||||
|
||||
self.first = true;
|
||||
for (key, value) in iter {
|
||||
try!(self.serialize_sep());
|
||||
try!(key.serialize(self));
|
||||
try!(self.wr.write_str(": "));
|
||||
try!(write!(&mut self.wr, ": "));
|
||||
try!(value.serialize(self));
|
||||
}
|
||||
|
||||
@ -577,9 +577,9 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_writer<
|
||||
W: Writer,
|
||||
T: Serialize<Serializer<W>, IoError>
|
||||
>(writer: W, value: &T) -> IoResult<W> {
|
||||
W: io::Write,
|
||||
T: Serialize<Serializer<W>, io::Error>
|
||||
>(writer: W, value: &T) -> io::Result<W> {
|
||||
let mut serializer = Serializer::new(writer);
|
||||
try!(value.serialize(&mut serializer));
|
||||
Ok(serializer.unwrap())
|
||||
@ -588,7 +588,7 @@ pub fn to_writer<
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
#[inline]
|
||||
pub fn to_vec<
|
||||
T: Serialize<Serializer<Vec<u8>>, IoError>
|
||||
T: Serialize<Serializer<Vec<u8>>, io::Error>
|
||||
>(value: &T) -> Vec<u8> {
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
@ -599,7 +599,7 @@ pub fn to_vec<
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
#[inline]
|
||||
pub fn to_string<
|
||||
T: Serialize<Serializer<Vec<u8>>, IoError>
|
||||
T: Serialize<Serializer<Vec<u8>>, io::Error>
|
||||
>(value: &T) -> Result<String, FromUtf8Error> {
|
||||
let buf = to_vec(value);
|
||||
String::from_utf8(buf)
|
||||
@ -608,9 +608,9 @@ pub fn to_string<
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_pretty_writer<
|
||||
W: Writer,
|
||||
T: Serialize<PrettySerializer<W>, IoError>
|
||||
>(writer: W, value: &T) -> IoResult<W> {
|
||||
W: io::Write,
|
||||
T: Serialize<PrettySerializer<W>, io::Error>
|
||||
>(writer: W, value: &T) -> io::Result<W> {
|
||||
let mut serializer = PrettySerializer::new(writer);
|
||||
try!(value.serialize(&mut serializer));
|
||||
Ok(serializer.unwrap())
|
||||
@ -618,7 +618,7 @@ pub fn to_pretty_writer<
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
pub fn to_pretty_vec<
|
||||
T: Serialize<PrettySerializer<Vec<u8>>, IoError>
|
||||
T: Serialize<PrettySerializer<Vec<u8>>, io::Error>
|
||||
>(value: &T) -> Vec<u8> {
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
@ -628,7 +628,7 @@ pub fn to_pretty_vec<
|
||||
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
pub fn to_pretty_string<
|
||||
T: Serialize<PrettySerializer<Vec<u8>>, IoError>
|
||||
T: Serialize<PrettySerializer<Vec<u8>>, io::Error>
|
||||
>(value: &T) -> Result<String, FromUtf8Error> {
|
||||
let buf = to_pretty_vec(value);
|
||||
String::from_utf8(buf)
|
||||
|
@ -1,7 +1,6 @@
|
||||
use std::collections::{HashMap, BTreeMap, btree_map};
|
||||
use std::fmt;
|
||||
use std::old_io::{ByRefWriter, IoResult};
|
||||
use std::old_io;
|
||||
use std::io::{self, WriteExt};
|
||||
use std::str;
|
||||
use std::string::ToString;
|
||||
use std::vec;
|
||||
@ -27,14 +26,14 @@ pub enum Value {
|
||||
|
||||
impl Value {
|
||||
/// Serializes a json value into an io::writer. Uses a single line.
|
||||
pub fn to_writer<W: Writer>(&self, wr: W) -> IoResult<()> {
|
||||
pub fn to_writer<W: io::Write>(&self, wr: W) -> io::Result<()> {
|
||||
let mut serializer = Serializer::new(wr);
|
||||
self.serialize(&mut serializer)
|
||||
}
|
||||
|
||||
/// Serializes a json value into an io::writer.
|
||||
/// Pretty-prints in a more readable format.
|
||||
pub fn to_pretty_writer<W: Writer>(&self, wr: W) -> IoResult<()> {
|
||||
pub fn to_pretty_writer<W: io::Write>(&self, wr: W) -> io::Result<()> {
|
||||
let mut serializer = PrettySerializer::new(wr);
|
||||
self.serialize(&mut serializer)
|
||||
}
|
||||
@ -220,9 +219,16 @@ struct WriterFormatter<'a, 'b: 'a> {
|
||||
inner: &'a mut fmt::Formatter<'b>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Writer for WriterFormatter<'a, 'b> {
|
||||
fn write_all(&mut self, buf: &[u8]) -> IoResult<()> {
|
||||
self.inner.write_str(str::from_utf8(buf).unwrap()).map_err(|_| old_io::IoError::last_error())
|
||||
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match self.inner.write_str(str::from_utf8(buf).unwrap()) {
|
||||
Ok(_) => Ok(buf.len()),
|
||||
Err(_) => Err(io::Error::last_os_error()),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ extern crate test;
|
||||
extern crate serde;
|
||||
|
||||
use std::fmt::Debug;
|
||||
use std::old_io;
|
||||
use std::io;
|
||||
use std::str;
|
||||
use std::string;
|
||||
use std::collections::BTreeMap;
|
||||
@ -113,7 +113,7 @@ impl ToJson for Outer {
|
||||
}
|
||||
|
||||
fn test_encode_ok<
|
||||
T: PartialEq + Debug + ToJson + ser::Serialize<json::Serializer<Vec<u8>>, old_io::IoError>
|
||||
T: PartialEq + Debug + ToJson + ser::Serialize<json::Serializer<Vec<u8>>, io::Error>
|
||||
>(errors: &[(T, &str)]) {
|
||||
for &(ref value, out) in errors {
|
||||
let out = out.to_string();
|
||||
@ -127,7 +127,7 @@ fn test_encode_ok<
|
||||
}
|
||||
|
||||
fn test_pretty_encode_ok<
|
||||
T: PartialEq + Debug + ToJson + ser::Serialize<json::PrettySerializer<Vec<u8>>, old_io::IoError>
|
||||
T: PartialEq + Debug + ToJson + ser::Serialize<json::PrettySerializer<Vec<u8>>, io::Error>
|
||||
>(errors: &[(T, &str)]) {
|
||||
for &(ref value, out) in errors {
|
||||
let out = out.to_string();
|
||||
|
Loading…
Reference in New Issue
Block a user