forked from furnace-dev/sonic-mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mojo
93 lines (74 loc) · 2.41 KB
/
main.mojo
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from sonic import JsonObject
from sonic import JsonArray
fn test_read_json() raises:
# Create a JSON object
var o = JsonObject(
'{"i64": 1000, "u64": 1000000000000000000, "b": true, "s": "Hi", "obj":'
' {"a": 100, "s": "hello"}, "arr": [1,2,3], "s_arr": ["a", "b", "c"],'
' "null": null}'
)
# Reading
var i64 = o.get_i64("i64")
var u64 = o.get_u64("u64")
var b = o.get_bool("b")
var s = o.get_str("s")
# Reading nested object
var obj = o.get_object_mut("obj")
var obj_a = obj.get_i64("a")
var obj_s = obj.get_str("s")
# Reading array
var arr = o.get_array_mut("arr")
var arr_0 = arr.get_i64(0)
var arr_1 = arr.get_i64(1)
var arr_2 = arr.get_i64(2)
# Reading string array
var s_arr = o.get_array_mut("s_arr")
var s_arr_len = s_arr.len()
var s_arr_0 = s_arr.get_str(0)
var s_arr_1 = s_arr.get_str(1)
var s_arr_2 = s_arr.get_str(2)
# Handling null value
var null = o.get_value("null")
var i64_2 = o.get_value("i64")
# Printing results
print("i64: ", i64)
print("u64: ", u64)
print("bool: ", b)
print("string: ", s)
print("obj.a: ", obj_a)
print("obj.s: ", obj_s)
print("arr[0]: ", arr_0)
print("arr[1]: ", arr_1)
print("arr[2]: ", arr_2)
print("s_arr length: ", s_arr_len)
print("s_arr[0]: ", s_arr_0)
print("s_arr[1]: ", s_arr_1)
print("s_arr[2]: ", s_arr_2)
print("null is null: ", null.is_null())
print("i64 is null: ", i64_2.is_null())
fn test_write_json() raises:
# Create a JSON object
var o = JsonObject('{"a": {"b": {"c": 100}}}')
# Get the nested object
var a = o.get_object_mut("a")
var b = a.get_object_mut("b")
# Read the current value
var c = b.get_i64("c")
# Output the current value
print("Initial value of c: ", c)
# Write a new value
o.insert_bool("f", True)
o.insert_u64("g", 1000000000000000000)
o.insert_i64("d", 101)
o.insert_str("e", "hello")
o.insert_array("h", JsonArray())
o.insert_object("i", JsonObject('{"j": 100}'))
# Get the updated object
var a_2 = o.get_object_mut("a")
# Print the string representation of the nested object
print("Updated object a: ", a_2.to_string())
# Print the string representation of the entire JSON object
print("Updated JSON object: ", o.to_string())
fn main() raises:
test_read_json()
test_write_json()