From 85bf3b798f6d52c374c35f7fbe47df132891d3b2 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Fri, 30 Dec 2011 17:48:12 +1030 Subject: Add support for Lua 5.2 and cjson.new Update all Lua scripts to use new module init style everywhere: local json = require "cjson" Lua CJSON does not register a global table under Lua 5.2. The global table can be disabled under Lua 5.1 with DISABLE_CJSON_GLOBAL. Other changes: - Store CJSON configuration as an upvalue for each function. - Add "cjson.new" function to create another module table with a separate configuration. - Add _NAME and _VERSION variables. --- tests/bench.lua | 2 +- tests/common.lua | 6 +++--- tests/decode.lua | 4 ++-- tests/encode.lua | 4 ++-- tests/test.lua | 16 ++++++++-------- 5 files changed, 16 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/bench.lua b/tests/bench.lua index fdd0bb0..c81213d 100755 --- a/tests/bench.lua +++ b/tests/bench.lua @@ -72,7 +72,7 @@ function bench_file(filename) return benchmark(tests, 0.1, 5) end -cjson.encode_keep_buffer(true) +json.encode_keep_buffer(true) for i = 1, #arg do local results = bench_file(arg[i]) diff --git a/tests/common.lua b/tests/common.lua index f3dc6f7..7472a10 100644 --- a/tests/common.lua +++ b/tests/common.lua @@ -1,4 +1,4 @@ -require "cjson" +local json = require "cjson" -- Misc routines to assist with CJSON testing -- @@ -77,8 +77,8 @@ function serialise_value(value, indent, depth) if indent == nil then indent = "" end if depth == nil then depth = 0 end - if value == cjson.null then - return "cjson.null" + if value == json.null then + return "json.null" elseif type(value) == "string" then return string.format("%q", value) elseif type(value) == "nil" or type(value) == "number" or diff --git a/tests/decode.lua b/tests/decode.lua index cac29e6..89354cd 100755 --- a/tests/decode.lua +++ b/tests/decode.lua @@ -7,8 +7,8 @@ -- ./decode.lua test.json require "common" -require "cjson" +local json = require "cjson" local json_text = file_load(arg[1]) -local t = cjson.decode(json_text) +local t = json.decode(json_text) print(serialise_value(t)) diff --git a/tests/encode.lua b/tests/encode.lua index f13787c..a8d749a 100755 --- a/tests/encode.lua +++ b/tests/encode.lua @@ -7,7 +7,7 @@ -- ./encode.lua lua_data.lua require "common" -require "cjson" +local json = require "cjson" function get_lua_table(file) local func = loadstring("data = " .. file_load(file)) @@ -23,6 +23,6 @@ function get_lua_table(file) end local t = get_lua_table(arg[1]) -print(cjson.encode(t)) +print(json.encode(t)) -- vi:ai et sw=4 ts=4: diff --git a/tests/test.lua b/tests/test.lua index bdae6ea..99ac73a 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -112,8 +112,8 @@ local decode_numeric_tests = { local encode_table_tests = { function() - cjson.encode_sparse_array(true, 2, 3) - cjson.encode_max_depth(5) + json.encode_sparse_array(true, 2, 3) + json.encode_max_depth(5) return "Setting sparse array (true, 2, 3) / max depth (5)" end, { json.encode, { { [3] = "sparse test" } }, @@ -211,19 +211,19 @@ local escape_tests = { local locale_tests = { function () os.setlocale("cs_CZ") - cjson.update_locale() + json.update_locale() return "Setting locale to cs_CZ (comma separator)" end, { json.encode, { 1.5 }, true, { '1.5' } }, { json.decode, { "[ 10, \"test\" ]" }, true, { { 10, "test" } } }, function () os.setlocale("C") - cjson.update_locale() + json.update_locale() return "Reverting locale to POSIX" end } -print(string.format("Testing Lua CJSON version %s\n", cjson.version)) +print(string.format("Testing Lua CJSON version %s\n", json.version)) run_test_group("decode simple value", decode_simple_tests) run_test_group("encode simple value", encode_simple_tests) @@ -232,7 +232,7 @@ run_test_group("decode numeric", decode_numeric_tests) -- INCLUDE: -- - Sparse array exception.. -- - .. --- cjson.encode_sparse_array(true, 2, 3) +-- json.encode_sparse_array(true, 2, 3) run_test_group("encode table", encode_table_tests) run_test_group("decode error", decode_error_tests) @@ -240,8 +240,8 @@ run_test_group("encode error", encode_error_tests) run_test_group("escape", escape_tests) run_test_group("locale", locale_tests) -cjson.refuse_invalid_numbers(false) -cjson.encode_max_depth(20) +json.refuse_invalid_numbers(false) +json.encode_max_depth(20) for i = 1, #arg do run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, true, { true }) -- cgit v1.2.3-55-g6feb