json: add "null"
This commit is contained in:
parent
7bf12f3723
commit
6f5a0a3b3b
@ -34,6 +34,8 @@ tag json {
|
||||
list(@[json]);
|
||||
/* Variant: dict */
|
||||
dict(map::hashmap<str,json>);
|
||||
/* Variant: null */
|
||||
null;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -233,6 +235,14 @@ fn from_str_bool(s: str) -> (option::t<json>, str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_str_null(s: str) -> (option::t<json>, 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<json>, 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<json>, 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); }
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user