diff --git a/serde_macros/src/de.rs b/serde_macros/src/de.rs index d86c337b..324da9ba 100644 --- a/serde_macros/src/de.rs +++ b/serde_macros/src/de.rs @@ -731,6 +731,9 @@ fn deserialize_map( } $extract_values + + try!(visitor.end()); + Ok($result) }) } diff --git a/src/de.rs b/src/de.rs index bf99b582..500bab4b 100644 --- a/src/de.rs +++ b/src/de.rs @@ -629,6 +629,8 @@ impl Visitor for VecVisitor where T: Deserialize { values.push(value); } + try!(visitor.end()); + Ok(values) } } @@ -735,6 +737,8 @@ impl Visitor for HashMapVisitor values.insert(key, value); } + try!(visitor.end()); + Ok(values) } } @@ -781,6 +785,8 @@ impl Visitor for BTreeMapVisitor values.insert(key, value); } + try!(visitor.end()); + Ok(values) } } diff --git a/src/json/de.rs b/src/json/de.rs index 3e6471bb..8c3d8983 100644 --- a/src/json/de.rs +++ b/src/json/de.rs @@ -466,7 +466,6 @@ impl<'a, Iter> de::SeqVisitor for SeqVisitor<'a, Iter> self.de.parse_whitespace(); if self.de.ch_is(b']') { - self.de.bump(); return Ok(None); } @@ -525,7 +524,6 @@ impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter> self.de.parse_whitespace(); if self.de.ch_is(b'}') { - self.de.bump(); return Ok(None); } @@ -565,11 +563,11 @@ impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter> fn end(&mut self) -> Result<(), Error> { self.de.parse_whitespace(); - if self.de.ch_is(b']') { + if self.de.ch_is(b'}') { self.de.bump(); Ok(()) } else if self.de.eof() { - Err(self.de.error(ErrorCode::EOFWhileParsingList)) + Err(self.de.error(ErrorCode::EOFWhileParsingObject)) } else { Err(self.de.error(ErrorCode::TrailingCharacters)) } diff --git a/tests/test_de.rs b/tests/test_de.rs index 0c07c7af..0ae59097 100644 --- a/tests/test_de.rs +++ b/tests/test_de.rs @@ -184,12 +184,13 @@ impl<'a, 'b> de::SeqVisitor for TokenDeserializerSeqVisitor<'a, 'b> { let first = self.first; self.first = false; - match self.de.tokens.next() { - Some(Token::SeqSep(first_)) if first_ == first => { + match self.de.tokens.peek() { + Some(&Token::SeqSep(first_)) if first_ == first => { self.len -= 1; + self.de.tokens.next(); Ok(Some(try!(Deserialize::deserialize(self.de)))) } - Some(Token::SeqEnd) => Ok(None), + Some(&Token::SeqEnd) => Ok(None), Some(_) => { Err(Error::SyntaxError) } @@ -198,6 +199,7 @@ impl<'a, 'b> de::SeqVisitor for TokenDeserializerSeqVisitor<'a, 'b> { } fn end(&mut self) -> Result<(), Error> { + assert_eq!(self.len, 0); match self.de.tokens.next() { Some(Token::SeqEnd) => Ok(()), Some(_) => Err(Error::SyntaxError), @@ -227,11 +229,13 @@ impl<'a, 'b> de::MapVisitor for TokenDeserializerMapVisitor<'a, 'b> { let first = self.first; self.first = false; - match self.de.tokens.next() { - Some(Token::MapSep(first_)) if first_ == first => { + match self.de.tokens.peek() { + Some(&Token::MapSep(first_)) if first_ == first => { + self.de.tokens.next(); + self.len -= 1; Ok(Some(try!(Deserialize::deserialize(self.de)))) } - Some(Token::MapEnd) => Ok(None), + Some(&Token::MapEnd) => Ok(None), Some(_) => Err(Error::SyntaxError), None => Err(Error::EndOfStreamError), } @@ -244,6 +248,7 @@ impl<'a, 'b> de::MapVisitor for TokenDeserializerMapVisitor<'a, 'b> { } fn end(&mut self) -> Result<(), Error> { + assert_eq!(self.len, 0); match self.de.tokens.next() { Some(Token::MapEnd) => Ok(()), Some(_) => Err(Error::SyntaxError),