-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_basic.fn
More file actions
27 lines (21 loc) · 775 Bytes
/
Copy pathjson_basic.fn
File metadata and controls
27 lines (21 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Typed JSON: parse text into a JsonValue, read fields with typed accessors, and
// render back with stringify.
imp std.c.io;
imp std.json;
imp std.result;
fun main() num {
let src = "{\"name\": \"fun\", \"version\": 1, \"stable\": true}";
fit parse(src) {
Result.Ok(doc) -> {
// get(key) returns Option<JsonValue>; the as_* accessors narrow the type.
let name = doc.get("name").unwrap_or(JsonValue.Null);
printf("name=%s\n", name.as_str().unwrap_or("?"));
let version = doc.get("version").unwrap_or(JsonValue.Null);
printf("version=%g\n", version.as_num().unwrap_or(0.0));
// Round-trips back to JSON text.
printf("json=%s\n", doc.to_string());
}
Result.Err(e) -> { printf("parse failed\n"); }
}
ret 0;
}