awtk/tests/fscripts/demo_json.fs

33 lines
668 B
Forth
Raw Normal View History

2021-01-07 16:17:01 +08:00
//load from string
2022-08-23 11:21:48 +08:00
var str='{
"name" : "fscript",
"age" : 100
2021-09-09 14:58:56 +08:00
}'
2022-08-23 11:21:48 +08:00
var a=json_load(str)
2021-01-07 16:17:01 +08:00
assert(object_get(a, "name")=="fscript");
assert(object_get(a, "age")==100);
//save to file
2022-08-23 11:21:48 +08:00
var b=json_save(a)
2021-01-07 16:17:01 +08:00
assert(file_write("test.json", b))
2021-09-09 14:58:56 +08:00
//json obj to string
2022-08-23 11:21:48 +08:00
var c=json_save_to_string(a)
2022-09-21 18:26:29 +08:00
print(c);
2021-09-09 14:58:56 +08:00
2021-01-07 16:17:01 +08:00
//load from file as text
a = json_load(file_read_text("test.json"))
assert(object_get(a, "name")=="fscript");
assert(object_get(a, "age")==100);
//load from file as binary
a = json_load(file_read_binary("test.json"))
assert(file_remove("test.json"))
assert(object_get(a, "name")=="fscript");
assert(object_get(a, "age")==100);
assert(file_remove("test.json"))