From 0f3ab84a261292d16f684551e67f2f007199936a Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Wed, 5 Oct 2011 23:30:27 +1030 Subject: Support locales which use comma decimal separators Some locales (cs_CZ, de_DE,..) use a comma as their decimal separator. This causes CJSON to generate incorrect JSON (Eg, [10,1]), and fail when parsing some valid JSON (Eg, [10,"test"]). Added USE_POSIX_LOCALE #define which harnesses the thread-safe POSIX.1-2008 locale support (newlocale(), uselocale(), freelocale()) to temporarily use the POSIX locale during JSON conversion. Some older POSIX operating systems with xlocale.h (MacOSX) are also supported. --- tests/test.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/test.lua b/tests/test.lua index 7a75243..d80dcf0 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -210,6 +210,21 @@ local escape_tests = { { json.decode, { utf16_escaped }, true, { utf8_raw } } } +-- The standard Lua interpreter is ANSI C online doesn't support locales +-- by default. Force a known problematic locale to test strtod()/sprintf(). +local locale_tests = { + function () + os.setlocale("cs_CZ") + 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") + return "Reverting locale to POSIX" + end +} + print(string.format("Testing CJSON v%s\n", cjson.version)) run_test_group("decode simple value", decode_simple_tests) @@ -225,6 +240,7 @@ run_test_group("encode table", encode_table_tests) run_test_group("decode error", decode_error_tests) 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) -- cgit v1.2.3-55-g6feb