1 # Test out Oil's JSON support.
2
3 #### json write STRING
4 json write ('foo')
5 var s = 'foo'
6 json write (s)
7 ## STDOUT:
8 "foo"
9 "foo"
10 ## END
11
12 #### json write ARRAY
13 json write (%(foo.cc foo.h))
14 json write --indent 0 (['foo.cc', 'foo.h'])
15 ## STDOUT:
16 [
17 "foo.cc",
18 "foo.h"
19 ]
20 [
21 "foo.cc",
22 "foo.h"
23 ]
24 ## END
25
26 #### json write compact format
27
28 # TODO: ORDER of keys should be PRESERVED
29
30 var mydict = {name: "bob", age: 30}
31
32 json write --pretty=0 (mydict)
33 # ignored
34 json write --pretty=F --indent 4 (mydict)
35 ## STDOUT:
36 {"age":30,"name":"bob"}
37 {"age":30,"name":"bob"}
38 ## END
39
40 #### json write in command sub
41 shopt -s oil:all # for echo
42 var mydict = {name: "bob", age: 30}
43 json write (mydict)
44 var x = $(json write (mydict))
45 echo $x
46 ## STDOUT:
47 {
48 "age": 30,
49 "name": "bob"
50 }
51 {
52 "age": 30,
53 "name": "bob"
54 }
55 ## END
56
57 #### json read passed invalid args
58 json read
59 echo status=$?
60 json read 'z z'
61 echo status=$?
62 json read a b c
63 echo status=$?
64 ## STDOUT:
65 status=2
66 status=2
67 status=2
68 ## END
69
70
71 #### json read with redirect
72 echo '{"age": 42}' > $TMP/foo.txt
73 json read :x < $TMP/foo.txt
74 pp cell :x
75 ## STDOUT:
76 x = (cell exported:F readonly:F nameref:F val:(value.Obj obj:{'age': 42}))
77 ## END
78
79 #### json read at end of pipeline (relies on lastpipe)
80 echo '{"age": 43}' | json read :y
81 pp cell y
82 ## STDOUT:
83 y = (cell exported:F readonly:F nameref:F val:(value.Obj obj:{'age': 43}))
84 ## END
85
86 #### invalid JSON
87 echo '{' | json read :y
88 echo pipeline status = $?
89 pp cell y
90 ## status: 1
91 ## STDOUT:
92 pipeline status = 1
93 ## END
94
95 #### json write expression
96 json write --pretty=0 ([1,2,3])
97 echo status=$?
98
99 json write (5, 6) # to many args
100 echo status=$?
101 ## STDOUT:
102 [1,2,3]
103 status=0
104 status=2
105 ## END
106
107 #### json write evaluation error
108
109 #var block = ^(echo hi)
110 #json write (block)
111 #echo status=$?
112
113 # undefined var
114 json write (a)
115 echo status=$?
116
117 ## status: 1
118 ## STDOUT:
119 ## END