diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-16 00:50:52 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-16 00:50:52 +0930 |
commit | 891a75e01fb0d419d3f8d4b3cf8c235325708a67 (patch) | |
tree | cb3f64fd01af774c76e696ee191b01d4c4b9ee30 | |
parent | 2cb7a17e3e5a0bab761661e193ffbea146dade73 (diff) | |
download | lua-cjson-891a75e01fb0d419d3f8d4b3cf8c235325708a67.tar.gz lua-cjson-891a75e01fb0d419d3f8d4b3cf8c235325708a67.tar.bz2 lua-cjson-891a75e01fb0d419d3f8d4b3cf8c235325708a67.zip |
Add command line encode/decode utilities
-rwxr-xr-x | tests/decode.lua | 11 | ||||
-rwxr-xr-x | tests/encode.lua | 26 |
2 files changed, 37 insertions, 0 deletions
diff --git a/tests/decode.lua b/tests/decode.lua new file mode 100755 index 0000000..c042168 --- /dev/null +++ b/tests/decode.lua | |||
@@ -0,0 +1,11 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | |||
3 | require "common" | ||
4 | require "cjson" | ||
5 | |||
6 | if not arg[1] then | ||
7 | print("usage: decode.lua FILE") | ||
8 | os.exit(-1) | ||
9 | end | ||
10 | |||
11 | print(serialise_value(cjson.decode(file_load(arg[1])))) | ||
diff --git a/tests/encode.lua b/tests/encode.lua new file mode 100755 index 0000000..e8026cc --- /dev/null +++ b/tests/encode.lua | |||
@@ -0,0 +1,26 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | |||
3 | require "common" | ||
4 | require "cjson" | ||
5 | |||
6 | function get_lua_table(file) | ||
7 | local func = loadstring("data = " .. file_load(file)) | ||
8 | if func == nil then | ||
9 | error("Invalid syntax? Lua table required.") | ||
10 | end | ||
11 | |||
12 | local env = {} | ||
13 | func = setfenv(func, env) | ||
14 | func() | ||
15 | |||
16 | return env.data | ||
17 | end | ||
18 | |||
19 | if not arg[1] then | ||
20 | print("usage: encode.lua FILE") | ||
21 | os.exit(-1) | ||
22 | end | ||
23 | |||
24 | print(cjson.encode(get_lua_table(arg[1]))) | ||
25 | |||
26 | -- vi:ai et sw=4 ts=4: | ||