1
Fork 0

Switch from serialization to std::serialize. (snapshot)

This commit is contained in:
Erick Tryzelaar 2012-12-17 19:31:04 -08:00
parent ec9305802b
commit 8650c6f683
19 changed files with 422 additions and 431 deletions

View file

@ -65,15 +65,15 @@ fn spaces(n: uint) -> ~str {
return ss;
}
pub struct Serializer {
pub struct Encoder {
priv wr: io::Writer,
}
pub fn Serializer(wr: io::Writer) -> Serializer {
Serializer { wr: wr }
pub fn Encoder(wr: io::Writer) -> Encoder {
Encoder { wr: wr }
}
pub impl Serializer: serialization::Serializer {
pub impl Encoder: serialize::Encoder {
fn emit_nil(&self) { self.wr.write_str("null") }
fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
@ -168,16 +168,16 @@ pub impl Serializer: serialization::Serializer {
}
}
pub struct PrettySerializer {
pub struct PrettyEncoder {
priv wr: io::Writer,
priv mut indent: uint,
}
pub fn PrettySerializer(wr: io::Writer) -> PrettySerializer {
PrettySerializer { wr: wr, indent: 0 }
pub fn PrettyEncoder(wr: io::Writer) -> PrettyEncoder {
PrettyEncoder { wr: wr, indent: 0 }
}
pub impl PrettySerializer: serialization::Serializer {
pub impl PrettyEncoder: serialize::Encoder {
fn emit_nil(&self) { self.wr.write_str("null") }
fn emit_uint(&self, v: uint) { self.emit_float(v as float); }
@ -283,21 +283,19 @@ pub impl PrettySerializer: serialization::Serializer {
}
}
pub impl<
S: serialization::Serializer
> Json: serialization::Serializable<S> {
fn serialize(&self, s: &S) {
pub impl<S: serialize::Encoder> Json: serialize::Encodable<S> {
fn encode(&self, s: &S) {
match *self {
Number(v) => v.serialize(s),
String(ref v) => v.serialize(s),
Boolean(v) => v.serialize(s),
List(ref v) => v.serialize(s),
Number(v) => v.encode(s),
String(ref v) => v.encode(s),
Boolean(v) => v.encode(s),
List(ref v) => v.encode(s),
Object(ref v) => {
do s.emit_rec || {
let mut idx = 0;
for v.each |key, value| {
do s.emit_field(*key, idx) {
value.serialize(s);
value.encode(s);
}
idx += 1;
}
@ -308,23 +306,23 @@ pub impl<
}
}
/// Serializes a json value into a io::writer
/// Encodes a json value into a io::writer
pub fn to_writer(wr: io::Writer, json: &Json) {
json.serialize(&Serializer(wr))
json.encode(&Encoder(wr))
}
/// Serializes a json value into a string
/// Encodes a json value into a string
pub pure fn to_str(json: &Json) -> ~str unsafe {
// ugh, should be safe
io::with_str_writer(|wr| to_writer(wr, json))
}
/// Serializes a json value into a io::writer
/// Encodes a json value into a io::writer
pub fn to_pretty_writer(wr: io::Writer, json: &Json) {
json.serialize(&PrettySerializer(wr))
json.encode(&PrettyEncoder(wr))
}
/// Serializes a json value into a string
/// Encodes a json value into a string
pub fn to_pretty_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_pretty_writer(wr, json))
}
@ -336,7 +334,7 @@ pub struct Parser {
priv mut col: uint,
}
/// Deserializes a json value from an io::reader
/// Decode a json value from an io::reader
pub fn Parser(rdr: io::Reader) -> Parser {
Parser {
rdr: rdr,
@ -695,28 +693,28 @@ priv impl Parser {
}
}
/// Deserializes a json value from an io::reader
/// Decodes a json value from an io::reader
pub fn from_reader(rdr: io::Reader) -> Result<Json, Error> {
Parser(rdr).parse()
}
/// Deserializes a json value from a string
/// Decodes a json value from a string
pub fn from_str(s: &str) -> Result<Json, Error> {
do io::with_str_reader(s) |rdr| {
from_reader(rdr)
}
}
pub struct Deserializer {
pub struct Decoder {
priv json: Json,
priv mut stack: ~[&Json],
}
pub fn Deserializer(json: Json) -> Deserializer {
Deserializer { json: move json, stack: ~[] }
pub fn Decoder(json: Json) -> Decoder {
Decoder { json: move json, stack: ~[] }
}
priv impl Deserializer {
priv impl Decoder {
fn peek(&self) -> &self/Json {
if self.stack.len() == 0 { self.stack.push(&self.json); }
vec::last(self.stack)
@ -728,7 +726,7 @@ priv impl Deserializer {
}
}
pub impl Deserializer: serialization::Deserializer {
pub impl Decoder: serialize::Decoder {
fn read_nil(&self) -> () {
debug!("read_nil");
match *self.pop() {