libstd: Skip trailing whitespaces after outer rparen

This makes "{ \"foo\": 1 }\n" and "[ 1 ]\n" valid json string
This commit is contained in:
Tycho Sci 2012-02-29 16:14:43 +09:00
parent cccb0fbf72
commit 0465d5217d

View File

@ -392,6 +392,7 @@ fn parse_list() -> result::t<json, error> {
if self.ch == ']' { if self.ch == ']' {
self.bump(); self.bump();
self.parse_whitespace();
ret ok(list(values)); ret ok(list(values));
} }
@ -406,7 +407,11 @@ fn parse_list() -> result::t<json, error> {
alt self.ch { alt self.ch {
',' { self.bump(); } ',' { self.bump(); }
']' { self.bump(); ret ok(list(values)); } ']' {
self.bump();
self.parse_whitespace();
ret ok(list(values));
}
_ { ret self.error("expecting ',' or ']'"); } _ { ret self.error("expecting ',' or ']'"); }
} }
} }
@ -422,6 +427,7 @@ fn parse_object() -> result::t<json, error> {
if self.ch == '}' { if self.ch == '}' {
self.bump(); self.bump();
self.parse_whitespace();
ret ok(dict(values)); ret ok(dict(values));
} }
@ -453,7 +459,11 @@ fn parse_object() -> result::t<json, error> {
alt self.ch { alt self.ch {
',' { self.bump(); } ',' { self.bump(); }
'}' { self.bump(); ret ok(dict(values)); } '}' {
self.bump();
self.parse_whitespace();
ret ok(dict(values));
}
_ { _ {
if self.eof() { break; } if self.eof() { break; }
ret self.error("expecting ',' or '}'"); ret self.error("expecting ',' or '}'");
@ -691,6 +701,7 @@ fn test_read_list() {
assert from_str("[ false ]") == ok(list([boolean(false)])); assert from_str("[ false ]") == ok(list([boolean(false)]));
assert from_str("[null]") == ok(list([null])); assert from_str("[null]") == ok(list([null]));
assert from_str("[3, 1]") == ok(list([num(3f), num(1f)])); assert from_str("[3, 1]") == ok(list([num(3f), num(1f)]));
assert from_str("\n[3, 2]\n") == ok(list([num(3f), num(2f)]));
assert from_str("[2, [4, 1]]") == assert from_str("[2, [4, 1]]") ==
ok(list([num(2f), list([num(4f), num(1f)])])); ok(list([num(2f), list([num(4f), num(1f)])]));
} }
@ -727,6 +738,8 @@ fn test_read_dict() {
assert eq(result::get(from_str("{ \"a\": null, \"b\" : true }")), assert eq(result::get(from_str("{ \"a\": null, \"b\" : true }")),
mk_dict([("a", null), ("b", boolean(true))])); mk_dict([("a", null), ("b", boolean(true))]));
assert eq(result::get(from_str("\n{ \"a\": null, \"b\" : true }\n")),
mk_dict([("a", null), ("b", boolean(true))]));
assert eq(result::get(from_str("{\"a\" : 1.0 ,\"b\": [ true ]}")), assert eq(result::get(from_str("{\"a\" : 1.0 ,\"b\": [ true ]}")),
mk_dict([ mk_dict([
("a", num(1.0)), ("a", num(1.0)),