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