diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index 0b14112e..b584aa15 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -10,8 +10,8 @@ use super::error::{Error, ErrorCode}; pub struct Parser { rdr: Iter, ch: Option, - line: uint, - col: uint, + line: usize, + col: usize, buf: Vec, } @@ -82,9 +82,8 @@ impl> Parser { #[inline] fn parse_value< - R, - V: de::Visitor, R, Error>, - >(&mut self, visitor: &mut V) -> Result { + V: de::Visitor, + >(&mut self, visitor: &mut V) -> Result { self.parse_whitespace(); if self.eof() { @@ -94,7 +93,7 @@ impl> Parser { match self.ch_or_null() { b'n' => { try!(self.parse_ident(b"ull")); - visitor.visit_null() + visitor.visit_unit() } b't' => { try!(self.parse_ident(b"rue")); @@ -107,7 +106,6 @@ impl> Parser { b'0' ... b'9' | b'-' => self.parse_number(visitor), b'"' => { try!(self.parse_string()); - //let s = String::from_utf8(self.buf.clone()).unwrap(); let s = str::from_utf8(self.buf.as_slice()).unwrap(); visitor.visit_str(s) } @@ -137,9 +135,8 @@ impl> Parser { #[inline] fn parse_number< - R, - V: de::Visitor, R, Error>, - >(&mut self, visitor: &mut V) -> Result { + V: de::Visitor, + >(&mut self, visitor: &mut V) -> Result { let mut neg = 1; if self.ch_is(b'-') { @@ -217,7 +214,7 @@ impl> Parser { match self.ch_or_null() { c @ b'0' ... b'9' => { dec /= 10.0; - res += (((c as int) - (b'0' as int)) as f64) * dec; + res += (((c as u64) - (b'0' as u64)) as f64) * dec; self.bump(); } _ => break, @@ -231,7 +228,7 @@ impl> Parser { fn parse_exponent(&mut self, mut res: f64) -> Result { self.bump(); - let mut exp = 0u; + let mut exp = 0; let mut neg_exp = false; if self.ch_is(b'+') { @@ -250,7 +247,7 @@ impl> Parser { match self.ch_or_null() { c @ b'0' ... b'9' => { exp *= 10; - exp += (c as uint) - (b'0' as uint); + exp += (c as i32) - (b'0' as i32); self.bump(); } @@ -258,7 +255,7 @@ impl> Parser { } } - let exp: f64 = 10_f64.powi(exp as i32); + let exp: f64 = 10_f64.powi(exp); if neg_exp { res /= exp; } else { @@ -270,9 +267,9 @@ impl> Parser { #[inline] fn decode_hex_escape(&mut self) -> Result { - let mut i = 0u; + let mut i = 0; let mut n = 0u16; - while i < 4u && !self.eof() { + while i < 4 && !self.eof() { self.bump(); n = match self.ch_or_null() { c @ b'0' ... b'9' => n * 16_u16 + ((c as u16) - (b'0' as u16)), @@ -285,11 +282,11 @@ impl> Parser { _ => { return Err(self.error(ErrorCode::InvalidEscape)); } }; - i += 1u; + i += 1; } // Error out if we didn't parse 4 digits. - if i != 4u { + if i != 4 { return Err(self.error(ErrorCode::InvalidEscape)); } @@ -380,12 +377,13 @@ impl> Parser { } } -impl> Deserializer for Parser { +impl> Deserializer for Parser { + type Error = Error; + #[inline] fn visit< - R, - V: de::Visitor, R, Error>, - >(&mut self, visitor: &mut V) -> Result { + V: de::Visitor, + >(&mut self, visitor: &mut V) -> Result { self.parse_value(visitor) } } @@ -395,9 +393,11 @@ struct SeqVisitor<'a, Iter: 'a> { first: bool, } -impl<'a, Iter: Iterator> de::SeqVisitor, Error> for SeqVisitor<'a, Iter> { +impl<'a, Iter: Iterator> de::SeqVisitor for SeqVisitor<'a, Iter> { + type Error = Error; + fn visit< - T: de::Deserialize, Error>, + T: de::Deserialize, >(&mut self) -> Result, Error> { self.parser.parse_whitespace(); @@ -439,9 +439,11 @@ struct MapVisitor<'a, Iter: 'a> { first: bool, } -impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVisitor<'a, Iter> { +impl<'a, Iter: Iterator> de::MapVisitor for MapVisitor<'a, Iter> { + type Error = Error; + fn visit_key< - K: de::Deserialize, Error>, + K: de::Deserialize, >(&mut self) -> Result, Error> { self.parser.parse_whitespace(); @@ -475,7 +477,7 @@ impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVis } fn visit_value< - V: de::Deserialize, Error>, + V: de::Deserialize, >(&mut self) -> Result { self.parser.parse_whitespace(); @@ -505,10 +507,10 @@ impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVis } /// Decodes a json value from an `Iterator`. -pub fn from_iter< - Iter: Iterator, - T: de::Deserialize, Error> ->(iter: Iter) -> Result { +pub fn from_iter(iter: I) -> Result + where I: Iterator, + T: de::Deserialize +{ let mut parser = Parser::new(iter); let value = try!(de::Deserialize::deserialize(&mut parser)); @@ -518,14 +520,12 @@ pub fn from_iter< } /// Decodes a json value from a string -pub fn from_str< - 'a, - T: de::Deserialize>, Error> ->(s: &'a str) -> Result { +pub fn from_str<'a, T>(s: &'a str) -> Result + where T: de::Deserialize +{ from_iter(s.bytes()) } -/* #[cfg(test)] mod tests { use std::str; @@ -544,10 +544,9 @@ mod tests { }) } - fn test_parse_ok< - 'a, - T: PartialEq + Show + Deserialize>, Error>, - >(errors: Vec<(&'a str, T)>) { + fn test_parse_ok<'a, T>(errors: Vec<(&'a str, T)>) + where T: PartialEq + Show + Deserialize, + { for (s, value) in errors.into_iter() { let v: Result = from_str(s); assert_eq!(v, Ok(value)); @@ -559,10 +558,9 @@ mod tests { } } - fn test_parse_err< - 'a, - T: PartialEq + Show + Deserialize>, Error> - >(errors: Vec<(&'a str, Error)>) { + fn test_parse_err<'a, T>(errors: Vec<(&'a str, Error)>) + where T: PartialEq + Show + Deserialize + { for (s, err) in errors.into_iter() { let v: Result = from_str(s); assert_eq!(v, Err(err)); @@ -668,12 +666,12 @@ mod tests { ]); test_parse_ok(vec![ - ("[3,1]", vec![3i, 1]), - ("[ 3 , 1 ]", vec![3i, 1]), + ("[3,1]", vec![3, 1]), + ("[ 3 , 1 ]", vec![3, 1]), ]); test_parse_ok(vec![ - ("[[3], [1, 2]]", vec![vec![3i], vec![1, 2]]), + ("[[3], [1, 2]]", vec![vec![3], vec![1, 2]]), ]); test_parse_ok(vec![ @@ -695,7 +693,7 @@ mod tests { #[test] fn test_parse_object() { - test_parse_err::>(vec![ + test_parse_err::>(vec![ ("{", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)), ("{ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)), ("{1", Error::SyntaxError(ErrorCode::KeyMustBeAString, 1, 2)), @@ -715,28 +713,27 @@ mod tests { ("{ }", treemap!()), ( "{\"a\":3}", - treemap!("a".to_string() => 3i) + treemap!("a".to_string() => 3) ), ( "{ \"a\" : 3 }", - treemap!("a".to_string() => 3i) + treemap!("a".to_string() => 3) ), ( "{\"a\":3,\"b\":4}", - treemap!("a".to_string() => 3i, "b".to_string() => 4) + treemap!("a".to_string() => 3, "b".to_string() => 4) ), ( "{ \"a\" : 3 , \"b\" : 4 }", - treemap!("a".to_string() => 3i, "b".to_string() => 4), + treemap!("a".to_string() => 3, "b".to_string() => 4), ), ]); test_parse_ok(vec![ ( "{\"a\": {\"b\": 3, \"c\": 4}}", - treemap!("a".to_string() => treemap!("b".to_string() => 3i, "c".to_string() => 4i)), + treemap!("a".to_string() => treemap!("b".to_string() => 3, "c".to_string() => 4)), ), ]); } } -*/ diff --git a/serde2/src/json/mod.rs b/serde2/src/json/mod.rs index dd211363..2c4e9035 100644 --- a/serde2/src/json/mod.rs +++ b/serde2/src/json/mod.rs @@ -8,8 +8,8 @@ pub use self::de::from_str; /* pub mod builder; -pub mod de; */ +pub mod de; pub mod error; pub mod ser; //pub mod value;