Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpx/lua-cjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-18 13:30:09 +0400
committerMark Pulford <mark@kyne.com.au>2012-03-04 12:24:35 +0400
commit1a5460be319ac29af31f201fbf9775340262ba9b (patch)
tree590312b65704b47c674b08efe14b8464aa58ce5b
parent90e08aa69d23df8c2eb3e231d57a46281d330f03 (diff)
Return boolean values from configuration functions
Return boolean values from configuration functions to simplify usage in the common case. Eg,: if not cjson.encode_invalid_numbers() then .. end
-rw-r--r--lua_cjson.c13
-rwxr-xr-xtests/test.lua22
2 files changed, 19 insertions, 16 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index b64ed62..0662414 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -235,23 +235,26 @@ static int json_integer_option(lua_State *l, int optindex, int *setting,
/* Process enumerated arguments for a configuration function */
static int json_enum_option(lua_State *l, int optindex, int *setting,
- const char **options, int bool_value)
+ const char **options, int bool_true)
{
static const char *bool_options[] = { "off", "on", NULL };
if (!options) {
options = bool_options;
- bool_value = 1;
+ bool_true = 1;
}
if (!lua_isnil(l, optindex)) {
- if (lua_isboolean(l, optindex))
- *setting = lua_toboolean(l, optindex) * bool_value;
+ if (bool_true && lua_isboolean(l, optindex))
+ *setting = lua_toboolean(l, optindex) * bool_true;
else
*setting = luaL_checkoption(l, optindex, NULL, options);
}
- lua_pushstring(l, options[*setting]);
+ if (bool_true && (*setting == 0 || *setting == bool_true))
+ lua_pushboolean(l, *setting);
+ else
+ lua_pushstring(l, options[*setting]);
return 1;
}
diff --git a/tests/test.lua b/tests/test.lua
index 8bb5b95..19fc1af 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -217,7 +217,7 @@ local cjson_tests = {
-- Test decoding invalid numbers
{ "Set decode_invalid_numbers(true)",
- json.decode_invalid_numbers, { true }, true, { "on" } },
+ json.decode_invalid_numbers, { true }, true, { true } },
{ "Decode hexadecimal",
json.decode, { '0x6' }, true, { 6 } },
{ "Decode +-Inf",
@@ -234,7 +234,7 @@ local cjson_tests = {
json.decode, { 'Noodle' },
false, { "Expected value but found invalid token at character 1" } },
{ "Set decode_invalid_numbers(false)",
- json.decode_invalid_numbers, { false }, true, { "off" } },
+ json.decode_invalid_numbers, { false }, true, { false } },
{ "Decode hexadecimal [throw error]",
json.decode, { '0x6' },
false, { "Expected value but found invalid number at character 1" } },
@@ -248,11 +248,11 @@ local cjson_tests = {
json.decode, { '[ +NaN, NaN, -NaN ]' },
false, { "Expected value but found invalid token at character 3" } },
{ 'Set decode_invalid_numbers("on")',
- json.decode_invalid_numbers, { "on" }, true, { "on" } },
+ json.decode_invalid_numbers, { "on" }, true, { true } },
-- Test encoding invalid numbers
{ "Set encode_invalid_numbers(false)",
- json.encode_invalid_numbers, { false }, true, { "off" } },
+ json.encode_invalid_numbers, { false }, true, { false } },
{ "Encode NaN [throw error]",
json.encode, { NaN },
false, { "Cannot serialise number: must not be NaN or Inf" } },
@@ -266,17 +266,17 @@ local cjson_tests = {
{ "Encode Infinity as null",
json.encode, { Inf }, true, { "null" } },
{ "Set encode_invalid_numbers(true)",
- json.encode_invalid_numbers, { true }, true, { "on" } },
+ json.encode_invalid_numbers, { true }, true, { true } },
{ "Encode NaN",
json.encode, { NaN }, true, { "nan" } },
{ "Encode Infinity",
json.encode, { Inf }, true, { "inf" } },
{ 'Set encode_invalid_numbers("off")',
- json.encode_invalid_numbers, { "off" }, true, { "off" } },
+ json.encode_invalid_numbers, { "off" }, true, { false } },
-- Test encoding tables
{ "Set encode_sparse_array(true, 2, 3)",
- json.encode_sparse_array, { true, 2, 3 }, true, { "on", 2, 3 } },
+ json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } },
{ "Encode sparse table as array #1",
json.encode, { { [3] = "sparse test" } },
true, { '[null,null,"sparse test"]' } },
@@ -290,7 +290,7 @@ local cjson_tests = {
json.encode, { { ["2"] = "numeric string key test" } },
true, { '{"2":"numeric string key test"}' } },
{ "Set encode_sparse_array(false)",
- json.encode_sparse_array, { false }, true, { "off", 2, 3 } },
+ json.encode_sparse_array, { false }, true, { false, 2, 3 } },
{ "Encode table with incompatible key [throw error]",
json.encode, { { [false] = "wrong" } },
false, { "Cannot serialise boolean: table key must be a number or string" } },
@@ -339,7 +339,7 @@ local cjson_tests = {
-- Test encode_keep_buffer() and enable_number_precision()
{ "Set encode_keep_buffer(false)",
- json.encode_keep_buffer, { false }, true, { "off" } },
+ json.encode_keep_buffer, { false }, true, { false } },
{ "Set encode_number_precision(3)",
json.encode_number_precision, { 3 }, true, { 3 } },
{ "Encode number with precision 3",
@@ -347,7 +347,7 @@ local cjson_tests = {
{ "Set encode_number_precision(14)",
json.encode_number_precision, { 14 }, true, { 14 } },
{ "Set encode_keep_buffer(true)",
- json.encode_keep_buffer, { true }, true, { "on" } },
+ json.encode_keep_buffer, { true }, true, { true } },
-- Test config API errors
-- Function is listed as '?' due to pcall
@@ -380,7 +380,7 @@ local cjson_tests = {
-- Wrap in a function to ensure the table returned by json.new() is used
{ "Check encode_sparse_array()",
function (...) return json.encode_sparse_array(...) end, { },
- true, { "off", 2, 10 } },
+ true, { false, 2, 10 } },
}
print(string.format("==> Testing Lua CJSON version %s\n", json._VERSION))