diff options
| -rw-r--r-- | lua_cjson.c | 13 | ||||
| -rwxr-xr-x | tests/test.lua | 22 |
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, | |||
| 235 | 235 | ||
| 236 | /* Process enumerated arguments for a configuration function */ | 236 | /* Process enumerated arguments for a configuration function */ |
| 237 | static int json_enum_option(lua_State *l, int optindex, int *setting, | 237 | static int json_enum_option(lua_State *l, int optindex, int *setting, |
| 238 | const char **options, int bool_value) | 238 | const char **options, int bool_true) |
| 239 | { | 239 | { |
| 240 | static const char *bool_options[] = { "off", "on", NULL }; | 240 | static const char *bool_options[] = { "off", "on", NULL }; |
| 241 | 241 | ||
| 242 | if (!options) { | 242 | if (!options) { |
| 243 | options = bool_options; | 243 | options = bool_options; |
| 244 | bool_value = 1; | 244 | bool_true = 1; |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | if (!lua_isnil(l, optindex)) { | 247 | if (!lua_isnil(l, optindex)) { |
| 248 | if (lua_isboolean(l, optindex)) | 248 | if (bool_true && lua_isboolean(l, optindex)) |
| 249 | *setting = lua_toboolean(l, optindex) * bool_value; | 249 | *setting = lua_toboolean(l, optindex) * bool_true; |
| 250 | else | 250 | else |
| 251 | *setting = luaL_checkoption(l, optindex, NULL, options); | 251 | *setting = luaL_checkoption(l, optindex, NULL, options); |
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | lua_pushstring(l, options[*setting]); | 254 | if (bool_true && (*setting == 0 || *setting == bool_true)) |
| 255 | lua_pushboolean(l, *setting); | ||
| 256 | else | ||
| 257 | lua_pushstring(l, options[*setting]); | ||
| 255 | 258 | ||
| 256 | return 1; | 259 | return 1; |
| 257 | } | 260 | } |
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 = { | |||
| 217 | 217 | ||
| 218 | -- Test decoding invalid numbers | 218 | -- Test decoding invalid numbers |
| 219 | { "Set decode_invalid_numbers(true)", | 219 | { "Set decode_invalid_numbers(true)", |
| 220 | json.decode_invalid_numbers, { true }, true, { "on" } }, | 220 | json.decode_invalid_numbers, { true }, true, { true } }, |
| 221 | { "Decode hexadecimal", | 221 | { "Decode hexadecimal", |
| 222 | json.decode, { '0x6' }, true, { 6 } }, | 222 | json.decode, { '0x6' }, true, { 6 } }, |
| 223 | { "Decode +-Inf", | 223 | { "Decode +-Inf", |
| @@ -234,7 +234,7 @@ local cjson_tests = { | |||
| 234 | json.decode, { 'Noodle' }, | 234 | json.decode, { 'Noodle' }, |
| 235 | false, { "Expected value but found invalid token at character 1" } }, | 235 | false, { "Expected value but found invalid token at character 1" } }, |
| 236 | { "Set decode_invalid_numbers(false)", | 236 | { "Set decode_invalid_numbers(false)", |
| 237 | json.decode_invalid_numbers, { false }, true, { "off" } }, | 237 | json.decode_invalid_numbers, { false }, true, { false } }, |
| 238 | { "Decode hexadecimal [throw error]", | 238 | { "Decode hexadecimal [throw error]", |
| 239 | json.decode, { '0x6' }, | 239 | json.decode, { '0x6' }, |
| 240 | false, { "Expected value but found invalid number at character 1" } }, | 240 | false, { "Expected value but found invalid number at character 1" } }, |
| @@ -248,11 +248,11 @@ local cjson_tests = { | |||
| 248 | json.decode, { '[ +NaN, NaN, -NaN ]' }, | 248 | json.decode, { '[ +NaN, NaN, -NaN ]' }, |
| 249 | false, { "Expected value but found invalid token at character 3" } }, | 249 | false, { "Expected value but found invalid token at character 3" } }, |
| 250 | { 'Set decode_invalid_numbers("on")', | 250 | { 'Set decode_invalid_numbers("on")', |
| 251 | json.decode_invalid_numbers, { "on" }, true, { "on" } }, | 251 | json.decode_invalid_numbers, { "on" }, true, { true } }, |
| 252 | 252 | ||
| 253 | -- Test encoding invalid numbers | 253 | -- Test encoding invalid numbers |
| 254 | { "Set encode_invalid_numbers(false)", | 254 | { "Set encode_invalid_numbers(false)", |
| 255 | json.encode_invalid_numbers, { false }, true, { "off" } }, | 255 | json.encode_invalid_numbers, { false }, true, { false } }, |
| 256 | { "Encode NaN [throw error]", | 256 | { "Encode NaN [throw error]", |
| 257 | json.encode, { NaN }, | 257 | json.encode, { NaN }, |
| 258 | false, { "Cannot serialise number: must not be NaN or Inf" } }, | 258 | false, { "Cannot serialise number: must not be NaN or Inf" } }, |
| @@ -266,17 +266,17 @@ local cjson_tests = { | |||
| 266 | { "Encode Infinity as null", | 266 | { "Encode Infinity as null", |
| 267 | json.encode, { Inf }, true, { "null" } }, | 267 | json.encode, { Inf }, true, { "null" } }, |
| 268 | { "Set encode_invalid_numbers(true)", | 268 | { "Set encode_invalid_numbers(true)", |
| 269 | json.encode_invalid_numbers, { true }, true, { "on" } }, | 269 | json.encode_invalid_numbers, { true }, true, { true } }, |
| 270 | { "Encode NaN", | 270 | { "Encode NaN", |
| 271 | json.encode, { NaN }, true, { "nan" } }, | 271 | json.encode, { NaN }, true, { "nan" } }, |
| 272 | { "Encode Infinity", | 272 | { "Encode Infinity", |
| 273 | json.encode, { Inf }, true, { "inf" } }, | 273 | json.encode, { Inf }, true, { "inf" } }, |
| 274 | { 'Set encode_invalid_numbers("off")', | 274 | { 'Set encode_invalid_numbers("off")', |
| 275 | json.encode_invalid_numbers, { "off" }, true, { "off" } }, | 275 | json.encode_invalid_numbers, { "off" }, true, { false } }, |
| 276 | 276 | ||
| 277 | -- Test encoding tables | 277 | -- Test encoding tables |
| 278 | { "Set encode_sparse_array(true, 2, 3)", | 278 | { "Set encode_sparse_array(true, 2, 3)", |
| 279 | json.encode_sparse_array, { true, 2, 3 }, true, { "on", 2, 3 } }, | 279 | json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } }, |
| 280 | { "Encode sparse table as array #1", | 280 | { "Encode sparse table as array #1", |
| 281 | json.encode, { { [3] = "sparse test" } }, | 281 | json.encode, { { [3] = "sparse test" } }, |
| 282 | true, { '[null,null,"sparse test"]' } }, | 282 | true, { '[null,null,"sparse test"]' } }, |
| @@ -290,7 +290,7 @@ local cjson_tests = { | |||
| 290 | json.encode, { { ["2"] = "numeric string key test" } }, | 290 | json.encode, { { ["2"] = "numeric string key test" } }, |
| 291 | true, { '{"2":"numeric string key test"}' } }, | 291 | true, { '{"2":"numeric string key test"}' } }, |
| 292 | { "Set encode_sparse_array(false)", | 292 | { "Set encode_sparse_array(false)", |
| 293 | json.encode_sparse_array, { false }, true, { "off", 2, 3 } }, | 293 | json.encode_sparse_array, { false }, true, { false, 2, 3 } }, |
| 294 | { "Encode table with incompatible key [throw error]", | 294 | { "Encode table with incompatible key [throw error]", |
| 295 | json.encode, { { [false] = "wrong" } }, | 295 | json.encode, { { [false] = "wrong" } }, |
| 296 | false, { "Cannot serialise boolean: table key must be a number or string" } }, | 296 | false, { "Cannot serialise boolean: table key must be a number or string" } }, |
| @@ -339,7 +339,7 @@ local cjson_tests = { | |||
| 339 | 339 | ||
| 340 | -- Test encode_keep_buffer() and enable_number_precision() | 340 | -- Test encode_keep_buffer() and enable_number_precision() |
| 341 | { "Set encode_keep_buffer(false)", | 341 | { "Set encode_keep_buffer(false)", |
| 342 | json.encode_keep_buffer, { false }, true, { "off" } }, | 342 | json.encode_keep_buffer, { false }, true, { false } }, |
| 343 | { "Set encode_number_precision(3)", | 343 | { "Set encode_number_precision(3)", |
| 344 | json.encode_number_precision, { 3 }, true, { 3 } }, | 344 | json.encode_number_precision, { 3 }, true, { 3 } }, |
| 345 | { "Encode number with precision 3", | 345 | { "Encode number with precision 3", |
| @@ -347,7 +347,7 @@ local cjson_tests = { | |||
| 347 | { "Set encode_number_precision(14)", | 347 | { "Set encode_number_precision(14)", |
| 348 | json.encode_number_precision, { 14 }, true, { 14 } }, | 348 | json.encode_number_precision, { 14 }, true, { 14 } }, |
| 349 | { "Set encode_keep_buffer(true)", | 349 | { "Set encode_keep_buffer(true)", |
| 350 | json.encode_keep_buffer, { true }, true, { "on" } }, | 350 | json.encode_keep_buffer, { true }, true, { true } }, |
| 351 | 351 | ||
| 352 | -- Test config API errors | 352 | -- Test config API errors |
| 353 | -- Function is listed as '?' due to pcall | 353 | -- Function is listed as '?' due to pcall |
| @@ -380,7 +380,7 @@ local cjson_tests = { | |||
| 380 | -- Wrap in a function to ensure the table returned by json.new() is used | 380 | -- Wrap in a function to ensure the table returned by json.new() is used |
| 381 | { "Check encode_sparse_array()", | 381 | { "Check encode_sparse_array()", |
| 382 | function (...) return json.encode_sparse_array(...) end, { }, | 382 | function (...) return json.encode_sparse_array(...) end, { }, |
| 383 | true, { "off", 2, 10 } }, | 383 | true, { false, 2, 10 } }, |
| 384 | } | 384 | } |
| 385 | 385 | ||
| 386 | print(string.format("==> Testing Lua CJSON version %s\n", json._VERSION)) | 386 | print(string.format("==> Testing Lua CJSON version %s\n", json._VERSION)) |
