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