Don't use trait objects in json
This commit is contained in:
parent
7926ac2778
commit
73b9a8cf74
17
bench_log.rs
17
bench_log.rs
@ -4,7 +4,6 @@ extern crate serialize;
|
|||||||
extern crate test;
|
extern crate test;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
|
|
||||||
use std::io::MemWriter;
|
|
||||||
use test::Bencher;
|
use test::Bencher;
|
||||||
|
|
||||||
use json;
|
use json;
|
||||||
@ -573,22 +572,10 @@ fn bench_encoder(b: &mut Bencher) {
|
|||||||
#[bench]
|
#[bench]
|
||||||
fn bench_serializer(b: &mut Bencher) {
|
fn bench_serializer(b: &mut Bencher) {
|
||||||
let log = Log::new();
|
let log = Log::new();
|
||||||
|
let _json = json::to_str(&log).unwrap();
|
||||||
let mut wr = MemWriter::with_capacity(700);
|
|
||||||
{
|
|
||||||
let mut serializer = json::Serializer::new(&mut wr);
|
|
||||||
log.serialize(&mut serializer).unwrap();
|
|
||||||
}
|
|
||||||
let json = String::from_utf8(wr.unwrap()).unwrap();
|
|
||||||
let _len = json.len();
|
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let mut wr = MemWriter::with_capacity(700);
|
let _json = json::to_str(&log).unwrap();
|
||||||
{
|
|
||||||
let mut serializer = json::Serializer::new(&mut wr);
|
|
||||||
log.serialize(&mut serializer).unwrap();
|
|
||||||
}
|
|
||||||
let _ = String::from_utf8(wr.unwrap());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
123
json.rs
123
json.rs
@ -71,7 +71,7 @@ fn main() {
|
|||||||
let to_encode_object = TestStruct{data_str:"example of string to encode".to_string()};
|
let to_encode_object = TestStruct{data_str:"example of string to encode".to_string()};
|
||||||
let mut m = io::MemWriter::new();
|
let mut m = io::MemWriter::new();
|
||||||
{
|
{
|
||||||
let mut serializer = json::Serializer::new(&mut m as &mut Writer);
|
let mut serializer = json::Serializer::new(m.by_ref());
|
||||||
match to_encode_object.encode(&mut serializer) {
|
match to_encode_object.encode(&mut serializer) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(e) => fail!("json encoding error: {}", e)
|
Err(e) => fail!("json encoding error: {}", e)
|
||||||
@ -260,23 +260,23 @@ pub type Object = TreeMap<String, Json>;
|
|||||||
|
|
||||||
impl Json {
|
impl Json {
|
||||||
/// Encodes a json value into an io::writer. Uses a single line.
|
/// Encodes a json value into an io::writer. Uses a single line.
|
||||||
pub fn to_writer(&self, wr: &mut Writer) -> EncodeResult {
|
pub fn to_writer<W: Writer>(&self, wr: W) -> EncodeResult {
|
||||||
let mut serializer = Serializer::new(wr);
|
let mut serializer = Serializer::new(wr);
|
||||||
self.serialize(&mut serializer)
|
self.serialize(&mut serializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encodes a json value into an io::writer.
|
/// Encodes a json value into an io::writer.
|
||||||
/// Pretty-prints in a more readable format.
|
/// Pretty-prints in a more readable format.
|
||||||
pub fn to_pretty_writer(&self, wr: &mut Writer) -> EncodeResult {
|
pub fn to_pretty_writer<W: Writer>(&self, wr: W) -> EncodeResult {
|
||||||
let mut serializer = PrettySerializer::new(wr);
|
let mut serializer = PrettySerializer::new(wr);
|
||||||
self.serialize(&mut serializer)
|
self.serialize(&mut serializer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encodes a json value into a string
|
/// Encodes a json value into a string
|
||||||
pub fn to_pretty_str(&self) -> String {
|
pub fn to_pretty_str(&self) -> String {
|
||||||
let mut s = MemWriter::new();
|
let mut wr = MemWriter::new();
|
||||||
self.to_pretty_writer(&mut s as &mut Writer).unwrap();
|
self.to_pretty_writer(wr.by_ref()).unwrap();
|
||||||
str::from_utf8(s.unwrap().as_slice()).unwrap().to_string()
|
str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the Json value is an Object, returns the value associated with the provided key.
|
/// If the Json value is an Object, returns the value associated with the provided key.
|
||||||
@ -414,7 +414,7 @@ impl Json {
|
|||||||
impl fmt::Show for Json {
|
impl fmt::Show for Json {
|
||||||
/// Encodes a json value into a string
|
/// Encodes a json value into a string
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.to_writer(f).map_err(|_| fmt::WriteError)
|
self.to_writer(f as &mut Writer).map_err(|_| fmt::WriteError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -733,7 +733,7 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
|
|||||||
|
|
||||||
pub type EncodeResult = io::IoResult<()>;
|
pub type EncodeResult = io::IoResult<()>;
|
||||||
|
|
||||||
fn escape_str(wr: &mut Writer, s: &str) -> Result<(), io::IoError> {
|
fn escape_str<W: Writer>(wr: &mut W, s: &str) -> Result<(), io::IoError> {
|
||||||
try!(wr.write_str("\""));
|
try!(wr.write_str("\""));
|
||||||
for byte in s.bytes() {
|
for byte in s.bytes() {
|
||||||
match byte {
|
match byte {
|
||||||
@ -750,7 +750,7 @@ fn escape_str(wr: &mut Writer, s: &str) -> Result<(), io::IoError> {
|
|||||||
wr.write_str("\"")
|
wr.write_str("\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spaces(wr: &mut Writer, n: uint) -> Result<(), io::IoError> {
|
fn spaces<W: Writer>(wr: &mut W, n: uint) -> Result<(), io::IoError> {
|
||||||
for _ in range(0, n) {
|
for _ in range(0, n) {
|
||||||
try!(wr.write_str(" "));
|
try!(wr.write_str(" "));
|
||||||
}
|
}
|
||||||
@ -766,39 +766,23 @@ enum SerializerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A structure for implementing serialization to JSON.
|
/// A structure for implementing serialization to JSON.
|
||||||
pub struct Serializer<'a> {
|
pub struct Serializer<W> {
|
||||||
wr: &'a mut Writer,
|
wr: W,
|
||||||
first: bool,
|
first: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Serializer<'a> {
|
impl<W: Writer> Serializer<W> {
|
||||||
/// Creates a new JSON serializer whose output will be written to the writer
|
/// Creates a new JSON serializer whose output will be written to the writer
|
||||||
/// specified.
|
/// specified.
|
||||||
pub fn new(wr: &'a mut Writer) -> Serializer<'a> {
|
pub fn new(wr: W) -> Serializer<W> {
|
||||||
Serializer {
|
Serializer {
|
||||||
wr: wr,
|
wr: wr,
|
||||||
first: true,
|
first: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode the specified struct into a json [u8]
|
|
||||||
pub fn buf_encode<T: ser::Serializable>(value: &T) -> Vec<u8> {
|
|
||||||
let mut wr = MemWriter::new();
|
|
||||||
{
|
|
||||||
let mut serializer = Serializer::new(&mut wr);
|
|
||||||
value.serialize(&mut serializer).unwrap();
|
|
||||||
}
|
|
||||||
wr.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encode the specified struct into a json str
|
|
||||||
pub fn str_encode<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
|
|
||||||
let buf = Serializer::buf_encode(value);
|
|
||||||
String::from_utf8(buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ser::Serializer<io::IoError> for Serializer<'a> {
|
impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
||||||
self.wr.write_str("null")
|
self.wr.write_str("null")
|
||||||
@ -875,12 +859,12 @@ impl<'a> ser::Serializer<io::IoError> for Serializer<'a> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
||||||
escape_str(self.wr, str::from_char(v).as_slice())
|
self.serialize_str(str::from_char(v).as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
||||||
escape_str(self.wr, v)
|
escape_str(&mut self.wr, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -1007,15 +991,15 @@ impl<'a> ser::Serializer<io::IoError> for Serializer<'a> {
|
|||||||
|
|
||||||
/// Another serializer for JSON, but prints out human-readable JSON instead of
|
/// Another serializer for JSON, but prints out human-readable JSON instead of
|
||||||
/// compact data
|
/// compact data
|
||||||
pub struct PrettySerializer<'a> {
|
pub struct PrettySerializer<W> {
|
||||||
wr: &'a mut Writer,
|
wr: W,
|
||||||
indent: uint,
|
indent: uint,
|
||||||
first: bool,
|
first: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PrettySerializer<'a> {
|
impl<W: Writer> PrettySerializer<W> {
|
||||||
/// Creates a new serializer whose output will be written to the specified writer
|
/// Creates a new serializer whose output will be written to the specified writer
|
||||||
pub fn new(wr: &'a mut Writer) -> PrettySerializer<'a> {
|
pub fn new(wr: W) -> PrettySerializer<W> {
|
||||||
PrettySerializer {
|
PrettySerializer {
|
||||||
wr: wr,
|
wr: wr,
|
||||||
indent: 0,
|
indent: 0,
|
||||||
@ -1023,22 +1007,6 @@ impl<'a> PrettySerializer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode the specified struct into a json [u8]
|
|
||||||
pub fn buf_encode<T: ser::Serializable>(value: &T) -> Vec<u8> {
|
|
||||||
let mut wr = MemWriter::new();
|
|
||||||
{
|
|
||||||
let mut serializer = PrettySerializer::new(&mut wr);
|
|
||||||
value.serialize(&mut serializer).unwrap();
|
|
||||||
}
|
|
||||||
wr.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encode the specified struct into a json str
|
|
||||||
pub fn str_encode<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
|
|
||||||
let buf = PrettySerializer::buf_encode(value);
|
|
||||||
String::from_utf8(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_sep(&mut self) -> Result<(), io::IoError> {
|
fn serialize_sep(&mut self) -> Result<(), io::IoError> {
|
||||||
if self.first {
|
if self.first {
|
||||||
@ -1049,7 +1017,7 @@ impl<'a> PrettySerializer<'a> {
|
|||||||
try!(self.wr.write_str(",\n"));
|
try!(self.wr.write_str(",\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
spaces(self.wr, self.indent)
|
spaces(&mut self.wr, self.indent)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -1057,7 +1025,7 @@ impl<'a> PrettySerializer<'a> {
|
|||||||
if !self.first {
|
if !self.first {
|
||||||
try!(self.wr.write_str("\n"));
|
try!(self.wr.write_str("\n"));
|
||||||
self.indent -= 2;
|
self.indent -= 2;
|
||||||
try!(spaces(self.wr, self.indent));
|
try!(spaces(&mut self.wr, self.indent));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.first = false;
|
self.first = false;
|
||||||
@ -1066,7 +1034,7 @@ impl<'a> PrettySerializer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ser::Serializer<io::IoError> for PrettySerializer<'a> {
|
impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
||||||
self.wr.write_str("null")
|
self.wr.write_str("null")
|
||||||
@ -1143,12 +1111,12 @@ impl<'a> ser::Serializer<io::IoError> for PrettySerializer<'a> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
||||||
escape_str(self.wr, str::from_char(v).as_slice())
|
self.serialize_str(str::from_char(v).as_slice())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
||||||
escape_str(self.wr, v)
|
escape_str(&mut self.wr, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -1253,6 +1221,38 @@ impl<'a> ser::Serializer<io::IoError> for PrettySerializer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Encode the specified struct into a json `[u8]` buffer.
|
||||||
|
pub fn to_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
|
||||||
|
let mut wr = MemWriter::new();
|
||||||
|
{
|
||||||
|
let mut serializer = Serializer::new(wr.by_ref());
|
||||||
|
value.serialize(&mut serializer).unwrap();
|
||||||
|
}
|
||||||
|
wr.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encode the specified struct into a json `String` buffer.
|
||||||
|
pub fn to_str<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
|
||||||
|
let buf = to_vec(value);
|
||||||
|
String::from_utf8(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encode the specified struct into a json `[u8]` buffer.
|
||||||
|
pub fn to_pretty_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
|
||||||
|
let mut wr = MemWriter::new();
|
||||||
|
{
|
||||||
|
let mut serializer = PrettySerializer::new(wr.by_ref());
|
||||||
|
value.serialize(&mut serializer).unwrap();
|
||||||
|
}
|
||||||
|
wr.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encode the specified struct into a json `String` buffer.
|
||||||
|
pub fn to_pretty_str<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
|
||||||
|
let buf = to_pretty_vec(value);
|
||||||
|
String::from_utf8(buf)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
/// The output of the streaming parser.
|
/// The output of the streaming parser.
|
||||||
#[deriving(Eq, Clone, Show)]
|
#[deriving(Eq, Clone, Show)]
|
||||||
@ -2190,7 +2190,6 @@ mod tests {
|
|||||||
use std::fmt::Show;
|
use std::fmt::Show;
|
||||||
use std::collections::TreeMap;
|
use std::collections::TreeMap;
|
||||||
|
|
||||||
use super::{Serializer, PrettySerializer};
|
|
||||||
use super::{Json, Null, Boolean, Number, String, List, Object};
|
use super::{Json, Null, Boolean, Number, String, List, Object};
|
||||||
use super::{ParserError, from_iter, from_str};
|
use super::{ParserError, from_iter, from_str};
|
||||||
use super::{from_json, ToJson};
|
use super::{from_json, ToJson};
|
||||||
@ -2476,10 +2475,10 @@ mod tests {
|
|||||||
for &(ref value, out) in errors.iter() {
|
for &(ref value, out) in errors.iter() {
|
||||||
let out = out.to_string();
|
let out = out.to_string();
|
||||||
|
|
||||||
let s = Serializer::str_encode(value).unwrap();
|
let s = super::to_str(value).unwrap();
|
||||||
assert_eq!(s, out);
|
assert_eq!(s, out);
|
||||||
|
|
||||||
let s = Serializer::str_encode(&value.to_json()).unwrap();
|
let s = super::to_str(&value.to_json()).unwrap();
|
||||||
assert_eq!(s, out);
|
assert_eq!(s, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2490,10 +2489,10 @@ mod tests {
|
|||||||
for &(ref value, out) in errors.iter() {
|
for &(ref value, out) in errors.iter() {
|
||||||
let out = out.to_string();
|
let out = out.to_string();
|
||||||
|
|
||||||
let s = PrettySerializer::str_encode(value).unwrap();
|
let s = super::to_pretty_str(value).unwrap();
|
||||||
assert_eq!(s, out);
|
assert_eq!(s, out);
|
||||||
|
|
||||||
let s = PrettySerializer::str_encode(&value.to_json()).unwrap();
|
let s = super::to_pretty_str(&value.to_json()).unwrap();
|
||||||
assert_eq!(s, out);
|
assert_eq!(s, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user