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