about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorTycho Sci <tychosci@gmail.com>2012-02-29 17:47:17 +0900
committerTycho Sci <tychosci@gmail.com>2012-02-29 17:47:17 +0900
commitecf87c3180898b7792e93fccccdf6a21f2d00508 (patch)
treedf96c8b9f907c17d8af7de1bfa92624761a427bb /src/libstd
parent0465d5217d7268aed71d1cc99bf025ee1fe748b5 (diff)
downloadrust-ecf87c3180898b7792e93fccccdf6a21f2d00508.tar.gz
rust-ecf87c3180898b7792e93fccccdf6a21f2d00508.zip
libstd: Skip trailing whitespaces after JSON value
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/json.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/src/libstd/json.rs b/src/libstd/json.rs
index 891cea99378..d069c36e866 100644
--- a/src/libstd/json.rs
+++ b/src/libstd/json.rs
@@ -154,6 +154,8 @@ impl parser for parser {
     fn parse() -> result::t<json, error> {
         alt self.parse_value() {
           ok(value) {
+            // Skip trailing whitespaces.
+            self.parse_whitespace();
             // Make sure there is no trailing characters.
             if self.eof() {
                 ok(value)
@@ -392,7 +394,6 @@ impl parser for parser {
 
         if self.ch == ']' {
             self.bump();
-            self.parse_whitespace();
             ret ok(list(values));
         }
 
@@ -407,11 +408,7 @@ impl parser for parser {
 
             alt self.ch {
               ',' { self.bump(); }
-              ']' {
-                  self.bump();
-                  self.parse_whitespace();
-                  ret ok(list(values));
-              }
+              ']' { self.bump(); ret ok(list(values)); }
               _ { ret self.error("expecting ',' or ']'"); }
             }
         }
@@ -427,7 +424,6 @@ impl parser for parser {
 
         if self.ch == '}' {
           self.bump();
-          self.parse_whitespace();
           ret ok(dict(values));
         }
 
@@ -459,11 +455,7 @@ impl parser for parser {
 
             alt self.ch {
               ',' { self.bump(); }
-              '}' {
-                  self.bump();
-                  self.parse_whitespace();
-                  ret ok(dict(values));
-              }
+              '}' { self.bump(); ret ok(dict(values)); }
               _ {
                   if self.eof() { break; }
                   ret self.error("expecting ',' or '}'");
@@ -637,6 +629,9 @@ mod tests {
         assert from_str("null") == ok(null);
         assert from_str("true") == ok(boolean(true));
         assert from_str("false") == ok(boolean(false));
+        assert from_str(" null ") == ok(null);
+        assert from_str(" true ") == ok(boolean(true));
+        assert from_str(" false ") == ok(boolean(false));
     }
 
     #[test]
@@ -664,6 +659,7 @@ mod tests {
         assert from_str("0.4e5") == ok(num(0.4e5f));
         assert from_str("0.4e+15") == ok(num(0.4e15f));
         assert from_str("0.4e-01") == ok(num(0.4e-01f));
+        assert from_str(" 3 ") == ok(num(3f));
     }
 
     #[test]
@@ -680,6 +676,7 @@ mod tests {
         assert from_str("\"\\n\"") == ok(string("\n"));
         assert from_str("\"\\r\"") == ok(string("\r"));
         assert from_str("\"\\t\"") == ok(string("\t"));
+        assert from_str(" \"foo\" ") == ok(string("foo"));
     }
 
     #[test]