Skip to content

Commit

Permalink
libstd: Skip trailing whitespaces after JSON value
Browse files Browse the repository at this point in the history
  • Loading branch information
tychosci committed Feb 29, 2012
1 parent 0465d52 commit ecf87c3
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/libstd/json.rs
Expand Up @@ -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)
Expand Down Expand Up @@ -392,7 +394,6 @@ impl parser for parser {

if self.ch == ']' {
self.bump();
self.parse_whitespace();
ret ok(list(values));
}

Expand All @@ -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 ']'"); }
}
}
Expand All @@ -427,7 +424,6 @@ impl parser for parser {

if self.ch == '}' {
self.bump();
self.parse_whitespace();
ret ok(dict(values));
}

Expand Down Expand Up @@ -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 '}'");
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit ecf87c3

Please sign in to comment.