v2 / vlib / json / cjson / cjson_test.v
23 lines · 20 sloc · 763 bytes · b0772193f83d7083bfe5eee6982f1516240b433b
Raw
1import json.cjson
2
3fn test_object_with_null() {
4 mut root := cjson.create_object()
5 root.add_item_to_object('name', cjson.create_string('Andre'))
6 root.add_item_to_object('age', cjson.create_null())
7 assert root.print_unformatted() == '{"name":"Andre","age":null}'
8 unsafe { cjson.delete(root) }
9}
10
11fn test_creating_complex_json() {
12 mut root := cjson.create_array()
13 root.add_item_to_array(cjson.create_string('user'))
14 mut obj := cjson.create_object()
15 obj.add_item_to_object('username', cjson.create_string('foo'))
16 obj.add_item_to_object('password', cjson.create_string('bar'))
17 root.add_item_to_array(obj)
18 result := root.print_unformatted()
19 println(result)
20
21 assert result == '["user",{"username":"foo","password":"bar"}]'
22 unsafe { cjson.delete(root) }
23}
24