diff --git a/json.rs b/json.rs index 903391ee..cab9663f 100644 --- a/json.rs +++ b/json.rs @@ -234,7 +234,9 @@ fn main() { */ use std::char; +use std::f64; use std::fmt; +use std::io::MemWriter; use std::io; use std::num; use std::str::ScalarValue; @@ -246,6 +248,8 @@ use std::vec; use de; use collections::{Deque, HashMap, RingBuf, TreeMap}; use collections::treemap; +use serialize; +use serialize::{Encoder, Encodable}; /// Represents a json value #[deriving(Clone, Eq, Show)] @@ -412,7 +416,10 @@ impl de::Deserializer for JsonDeserializer { // Special case treating enums as a String or a `{"variant": "...", "fields": [...]}`. #[inline] - fn expect_enum_start(&mut self, token: de::Token, _name: &str, variants: &[&str]) -> Result { + fn expect_enum_start(&mut self, + token: de::Token, + _name: &str, + variants: &[&str]) -> Result { let variant = match token { de::String(variant) => { self.stack.push(JsonDeserializerEndState); @@ -592,7 +599,6 @@ fn spaces(n: uint) -> String { return ss } -/* /// A structure for implementing serialization to JSON. pub struct Encoder<'a> { wr: &'a mut io::Writer, @@ -606,7 +612,10 @@ impl<'a> Encoder<'a> { } /// Encode the specified struct into a json [u8] - pub fn buffer_encode, io::IoError>>(to_encode_object: &T) -> Vec { + pub fn buffer_encode< + T: serialize::Encodable, + io::IoError> + >(to_encode_object: &T) -> Vec { //Serialize the object in a string using a writer let mut m = MemWriter::new(); { @@ -618,16 +627,16 @@ impl<'a> Encoder<'a> { } /// Encode the specified struct into a json str - pub fn str_encode, - io::IoError>>( - to_encode_object: &T) - -> String { + pub fn str_encode< + T: serialize::Encodable, + io::IoError> + >(to_encode_object: &T) -> String { let buff = Encoder::buffer_encode(to_encode_object); str::from_utf8(buff.as_slice()).unwrap().to_string() } } -impl<'a> ::Encoder for Encoder<'a> { +impl<'a> serialize::Encoder for Encoder<'a> { fn emit_nil(&mut self) -> EncodeResult { write!(self.wr, "null") } fn emit_uint(&mut self, v: uint) -> EncodeResult { self.emit_f64(v as f64) } @@ -820,7 +829,7 @@ impl<'a> PrettyEncoder<'a> { } } -impl<'a> ::Encoder for PrettyEncoder<'a> { +impl<'a> serialize::Encoder for PrettyEncoder<'a> { fn emit_nil(&mut self) -> EncodeResult { write!(self.wr, "null") } fn emit_uint(&mut self, v: uint) -> EncodeResult { self.emit_f64(v as f64) } @@ -1038,7 +1047,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> { } } -impl, S> Encodable for Json { +impl, S> serialize::Encodable for Json { fn encode(&self, e: &mut E) -> Result<(), S> { match *self { Number(v) => v.encode(e), @@ -1127,8 +1136,8 @@ impl Json { /// If the Json value is an Object, returns the associated TreeMap. /// Returns None otherwise. pub fn as_object<'a>(&'a self) -> Option<&'a Object> { - match self { - &Object(ref map) => Some(&**map), + match *self { + Object(ref map) => Some(map), _ => None } } @@ -1141,8 +1150,8 @@ impl Json { /// If the Json value is a List, returns the associated vector. /// Returns None otherwise. pub fn as_list<'a>(&'a self) -> Option<&'a List> { - match self { - &List(ref list) => Some(&*list), + match *self { + List(ref list) => Some(list), _ => None } } @@ -1169,8 +1178,8 @@ impl Json { /// If the Json value is a Number, returns the associated f64. /// Returns None otherwise. pub fn as_number(&self) -> Option { - match self { - &Number(n) => Some(n), + match *self { + Number(n) => Some(n), _ => None } } @@ -1183,8 +1192,8 @@ impl Json { /// If the Json value is a Boolean, returns the associated bool. /// Returns None otherwise. pub fn as_boolean(&self) -> Option { - match self { - &Boolean(b) => Some(b), + match *self { + Boolean(b) => Some(b), _ => None } } @@ -1197,13 +1206,12 @@ impl Json { /// If the Json value is a Null, returns (). /// Returns None otherwise. pub fn as_null(&self) -> Option<()> { - match self { - &Null => Some(()), + match *self { + Null => Some(()), _ => None } } } -*/ /* /// The output of the streaming parser. @@ -1239,6 +1247,7 @@ enum ParserState { ParseObjectValue, } +/* /// A Stack represents the current position of the parser in the logical /// structure of the JSON stream. /// For example foo.bar[3].x @@ -1380,6 +1389,7 @@ impl Stack { *self.stack.get_mut(len - 1) = InternalIndex(idx); } } +*/ /// A streaming JSON parser implemented as an iterator of JsonEvent, consuming /// an iterator of char. @@ -1857,7 +1867,7 @@ impl> de::Deserializer for Parser { #[inline] fn expect_enum_start(&mut self, token: de::Token, - name: &str, + _name: &str, variants: &[&str]) -> Result { // It's a little tricky to deserialize enums. Strings are simple to // parse, but objects require us to preparse the entire object because @@ -1870,7 +1880,7 @@ impl> de::Deserializer for Parser { variant } - de::MapStart(len) => { + de::MapStart(_len) => { let mut variant = None; let mut fields = None; @@ -2029,7 +2039,7 @@ pub fn from_iter< // Make sure the whole stream has been consumed. match parser.next() { - Some(Ok(token)) => parser.error(TrailingCharacters), + Some(Ok(_token)) => parser.error(TrailingCharacters), Some(Err(err)) => Err(err), None => Ok(value), } @@ -2058,12 +2068,6 @@ impl Decoder { } } -impl Decoder { - fn pop(&mut self) -> Json { - self.stack.pop().unwrap() - } -} - macro_rules! expect( ($e:expr, Null) => ({ match $e {