1 ## oils_failures_allowed: 1
2 ## tags: dev-minimal
3
4 #### json write STRING
5 shopt --set parse_proc
6
7 json write ('foo')
8 var s = 'foo'
9 json write (s)
10 ## STDOUT:
11 "foo"
12 "foo"
13 ## END
14
15 #### json write ARRAY
16 json write (:|foo.cc foo.h|)
17 json write --indent 0 (['foo.cc', 'foo.h'])
18 ## STDOUT:
19 [
20 "foo.cc",
21 "foo.h"
22 ]
23 [
24 "foo.cc",
25 "foo.h"
26 ]
27 ## END
28
29 #### json write Dict
30 json write ({k: 'v', k2: [4, 5]})
31
32 json write ([{k: 'v', k2: 'v2'}, {}])
33
34 ## STDOUT:
35 {
36 "k": "v",
37 "k2": [
38 4,
39 5
40 ]
41 }
42 [
43 {
44 "k": "v",
45 "k2": "v2"
46 },
47 {
48
49 }
50 ]
51 ## END
52
53 #### json write compact format
54 shopt --set parse_proc
55
56 # TODO: ORDER of keys should be PRESERVED
57 var mydict = {name: "bob", age: 30}
58
59 json write --pretty=0 (mydict)
60 # ignored
61 json write --pretty=F --indent 4 (mydict)
62 ## STDOUT:
63 {"name":"bob","age":30}
64 {"name":"bob","age":30}
65 ## END
66
67 #### json write in command sub
68 shopt -s oil:all # for echo
69 var mydict = {name: "bob", age: 30}
70 json write (mydict)
71 var x = $(json write (mydict))
72 echo $x
73 ## STDOUT:
74 {
75 "name": "bob",
76 "age": 30
77 }
78 {
79 "name": "bob",
80 "age": 30
81 }
82 ## END
83
84 #### json read passed invalid args
85 json read
86 echo status=$?
87 json read 'z z'
88 echo status=$?
89 json read a b c
90 echo status=$?
91 ## STDOUT:
92 status=2
93 status=2
94 status=2
95 ## END
96
97
98 #### json read with redirect
99 echo '{"age": 42}' > $TMP/foo.txt
100 json read :x < $TMP/foo.txt
101 pp cell :x
102 ## STDOUT:
103 x = (Cell exported:F readonly:F nameref:F val:(value.Dict d:[Dict age (value.Int i:42)]))
104 ## END
105
106 #### json read at end of pipeline (relies on lastpipe)
107 echo '{"age": 43}' | json read :y
108 pp cell y
109 ## STDOUT:
110 y = (Cell exported:F readonly:F nameref:F val:(value.Dict d:[Dict age (value.Int i:43)]))
111 ## END
112
113 #### invalid JSON
114 echo '{' | json read :y
115 echo pipeline status = $?
116 pp cell y
117 ## status: 1
118 ## STDOUT:
119 pipeline status = 1
120 ## END
121
122 #### json write expression
123 json write --pretty=0 ([1,2,3])
124 echo status=$?
125
126 json write (5, 6) # to many args
127 echo status=$?
128 ## STDOUT:
129 [1,2,3]
130 status=0
131 status=2
132 ## END
133
134 #### json write evaluation error
135
136 #var block = ^(echo hi)
137 #json write (block)
138 #echo status=$?
139
140 # undefined var
141 json write (a)
142 echo status=$?
143
144 ## status: 1
145 ## STDOUT:
146 ## END
147
148 #### json write of data structure with cycle
149 var L = [1, 2, 3]
150 setvar L[0] = L
151
152 # TODO: I guess it should exit with status 1 or 3
153 json write (L)
154
155 var d = {k: 'v'}
156 setvar d.k1 = 'v2'
157
158 # This makes it hang? But not interactively
159 #setvar d.k2 = d
160
161 = d
162 #json write (d)
163
164 ## STDOUT:
165 ## END
166
167 #### j8 write
168
169 # TODO: much better tests
170 j8 write ([3, "foo"])
171
172 ## STDOUT:
173 [
174 3,
175 "foo"
176 ]
177 ## END
178