Skip to content

Commit 6f5a0a3

Browse files
Lenny222marijnh
authored andcommitted
json: add "null"
1 parent 7bf12f3 commit 6f5a0a3

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/libstd/json.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ tag json {
3434
list(@[json]);
3535
/* Variant: dict */
3636
dict(map::hashmap<str,json>);
37+
/* Variant: null */
38+
null;
3739
}
3840

3941
/*
@@ -233,6 +235,14 @@ fn from_str_bool(s: str) -> (option::t<json>, str) {
233235
}
234236
}
235237

238+
fn from_str_null(s: str) -> (option::t<json>, str) {
239+
if (str::starts_with(s, "null")) {
240+
(some(null), str::slice(s, 4u, str::byte_len(s)))
241+
} else {
242+
(none, s)
243+
}
244+
}
245+
236246
fn from_str_helper(s: str) -> (option::t<json>, str) {
237247
let s = str::trim_left(s);
238248
if str::is_empty(s) { ret (none, s); }
@@ -243,6 +253,7 @@ fn from_str_helper(s: str) -> (option::t<json>, str) {
243253
'{' { from_str_dict(s) }
244254
'0' to '9' | '-' | '+' | '.' { from_str_float(s) }
245255
't' | 'f' { from_str_bool(s) }
256+
'n' { from_str_null(s) }
246257
_ { ret (none, s); }
247258
}
248259
}

src/test/stdtest/json.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import option;
55
import std::json::*;
66
import option::{none, some};
77

8+
#[test]
9+
fn test_from_str_null() {
10+
assert(from_str("null") == some(null));
11+
}
12+
813
#[test]
914
fn test_from_str_num() {
1015
assert(from_str("3") == some(num(3f)));
@@ -31,6 +36,7 @@ fn test_from_str_bool() {
3136
fn test_from_str_list() {
3237
assert(from_str("[]") == some(list(@[])));
3338
assert(from_str("[true]") == some(list(@[boolean(true)])));
39+
assert(from_str("[null]") == some(list(@[null])));
3440
assert(from_str("[3, 1]") == some(list(@[num(3f), num(1f)])));
3541
assert(from_str("[2, [4, 1]]") ==
3642
some(list(@[num(2f), list(@[num(4f), num(1f)])])));
@@ -44,6 +50,7 @@ fn test_from_str_list() {
4450
fn test_from_str_dict() {
4551
assert(from_str("{}") != none);
4652
assert(from_str("{\"a\": 3}") != none);
53+
assert(from_str("{\"a\": null}") != none);
4754
assert(from_str("{\"a\": }") == none);
4855
assert(from_str("{\"a\" }") == none);
4956
assert(from_str("{\"a\"") == none);

0 commit comments

Comments
 (0)