diff --git a/src/libstd/json.rs b/src/libstd/json.rs index e0b559ec747..2adfbca70cf 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -34,6 +34,8 @@ tag json { list(@[json]); /* Variant: dict */ dict(map::hashmap); + /* Variant: null */ + null; } /* @@ -233,6 +235,14 @@ fn from_str_bool(s: str) -> (option::t, str) { } } +fn from_str_null(s: str) -> (option::t, str) { + if (str::starts_with(s, "null")) { + (some(null), str::slice(s, 4u, str::byte_len(s))) + } else { + (none, s) + } +} + fn from_str_helper(s: str) -> (option::t, str) { let s = str::trim_left(s); if str::is_empty(s) { ret (none, s); } @@ -243,6 +253,7 @@ fn from_str_helper(s: str) -> (option::t, str) { '{' { from_str_dict(s) } '0' to '9' | '-' | '+' | '.' { from_str_float(s) } 't' | 'f' { from_str_bool(s) } + 'n' { from_str_null(s) } _ { ret (none, s); } } } diff --git a/src/test/stdtest/json.rs b/src/test/stdtest/json.rs index db65b223ff7..41031b2e16a 100644 --- a/src/test/stdtest/json.rs +++ b/src/test/stdtest/json.rs @@ -5,6 +5,11 @@ import option; import std::json::*; import option::{none, some}; +#[test] +fn test_from_str_null() { + assert(from_str("null") == some(null)); +} + #[test] fn test_from_str_num() { assert(from_str("3") == some(num(3f))); @@ -31,6 +36,7 @@ fn test_from_str_bool() { fn test_from_str_list() { assert(from_str("[]") == some(list(@[]))); assert(from_str("[true]") == some(list(@[boolean(true)]))); + assert(from_str("[null]") == some(list(@[null]))); assert(from_str("[3, 1]") == some(list(@[num(3f), num(1f)]))); assert(from_str("[2, [4, 1]]") == some(list(@[num(2f), list(@[num(4f), num(1f)])]))); @@ -44,6 +50,7 @@ fn test_from_str_list() { fn test_from_str_dict() { assert(from_str("{}") != none); assert(from_str("{\"a\": 3}") != none); + assert(from_str("{\"a\": null}") != none); assert(from_str("{\"a\": }") == none); assert(from_str("{\"a\" }") == none); assert(from_str("{\"a\"") == none);