diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:46:05 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:46:05 +0930 |
commit | 6ba7499ac5992c8cc849ffcbd6d2837d305f5b63 (patch) | |
tree | f7765d77bb51d3401f4fe0a9acc7279596928918 | |
parent | 22550d0ab3328554922822ca0207996b3d1bb73e (diff) | |
download | lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.tar.gz lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.tar.bz2 lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.zip |
Add support for stdin to encode.lua/decode.lua
-rw-r--r-- | tests/common.lua | 33 | ||||
-rwxr-xr-x | tests/decode.lua | 15 | ||||
-rwxr-xr-x | tests/encode.lua | 14 |
3 files changed, 42 insertions, 20 deletions
diff --git a/tests/common.lua b/tests/common.lua index e08b6c2..0b231bd 100644 --- a/tests/common.lua +++ b/tests/common.lua | |||
@@ -96,12 +96,21 @@ function dump_value(value) | |||
96 | end | 96 | end |
97 | 97 | ||
98 | function file_load(filename) | 98 | function file_load(filename) |
99 | local file, err = io.open(filename) | 99 | local file |
100 | if file == nil then | 100 | if filename == nil then |
101 | error("Unable to read " .. filename) | 101 | file = io.stdin |
102 | else | ||
103 | local err | ||
104 | file, err = io.open(filename) | ||
105 | if file == nil then | ||
106 | error(string.format("Unable to read '%s': %s", filename, err)) | ||
107 | end | ||
102 | end | 108 | end |
103 | local data = file:read("*a") | 109 | local data = file:read("*a") |
104 | file:close() | 110 | |
111 | if filename ~= nil then | ||
112 | file:close() | ||
113 | end | ||
105 | 114 | ||
106 | if data == nil then | 115 | if data == nil then |
107 | error("Failed to read " .. filename) | 116 | error("Failed to read " .. filename) |
@@ -111,12 +120,20 @@ function file_load(filename) | |||
111 | end | 120 | end |
112 | 121 | ||
113 | function file_save(filename, data) | 122 | function file_save(filename, data) |
114 | local file, err = io.open(filename, "w") | 123 | local file |
115 | if file == nil then | 124 | if filename == nil then |
116 | error("Unable to write " .. filename) | 125 | file = io.stdout |
126 | else | ||
127 | local err | ||
128 | file, err = io.open(filename, "w") | ||
129 | if file == nil then | ||
130 | error(string.format("Unable to write '%s': %s", filename, err)) | ||
131 | end | ||
117 | end | 132 | end |
118 | file:write(data) | 133 | file:write(data) |
119 | file:close() | 134 | if filename ~= nil then |
135 | file:close() | ||
136 | end | ||
120 | end | 137 | end |
121 | 138 | ||
122 | function compare_values(val1, val2) | 139 | function compare_values(val1, val2) |
diff --git a/tests/decode.lua b/tests/decode.lua index c042168..cac29e6 100755 --- a/tests/decode.lua +++ b/tests/decode.lua | |||
@@ -1,11 +1,14 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | -- usage: decode.lua [json_file] | ||
4 | -- | ||
5 | -- Eg: | ||
6 | -- echo '[ "testing" ]' | ./decode.lua | ||
7 | -- ./decode.lua test.json | ||
8 | |||
3 | require "common" | 9 | require "common" |
4 | require "cjson" | 10 | require "cjson" |
5 | 11 | ||
6 | if not arg[1] then | 12 | local json_text = file_load(arg[1]) |
7 | print("usage: decode.lua FILE") | 13 | local t = cjson.decode(json_text) |
8 | os.exit(-1) | 14 | print(serialise_value(t)) |
9 | end | ||
10 | |||
11 | print(serialise_value(cjson.decode(file_load(arg[1])))) | ||
diff --git a/tests/encode.lua b/tests/encode.lua index e8026cc..f13787c 100755 --- a/tests/encode.lua +++ b/tests/encode.lua | |||
@@ -1,5 +1,11 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | -- usage: encode.lua [lua_file] | ||
4 | -- | ||
5 | -- Eg: | ||
6 | -- echo '{ "testing" }' | ./encode.lua | ||
7 | -- ./encode.lua lua_data.lua | ||
8 | |||
3 | require "common" | 9 | require "common" |
4 | require "cjson" | 10 | require "cjson" |
5 | 11 | ||
@@ -16,11 +22,7 @@ function get_lua_table(file) | |||
16 | return env.data | 22 | return env.data |
17 | end | 23 | end |
18 | 24 | ||
19 | if not arg[1] then | 25 | local t = get_lua_table(arg[1]) |
20 | print("usage: encode.lua FILE") | 26 | print(cjson.encode(t)) |
21 | os.exit(-1) | ||
22 | end | ||
23 | |||
24 | print(cjson.encode(get_lua_table(arg[1]))) | ||
25 | 27 | ||
26 | -- vi:ai et sw=4 ts=4: | 28 | -- vi:ai et sw=4 ts=4: |