aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-10-05 23:30:27 +1030
committerMark Pulford <mark@kyne.com.au>2011-10-05 23:30:27 +1030
commit0f3ab84a261292d16f684551e67f2f007199936a (patch)
tree5ece4652a08b968232b417f5cf4ceaf234f2be2f /tests
parent03fa2b508aee4cdf2eac41d53834412ac757feef (diff)
downloadlua-cjson-0f3ab84a261292d16f684551e67f2f007199936a.tar.gz
lua-cjson-0f3ab84a261292d16f684551e67f2f007199936a.tar.bz2
lua-cjson-0f3ab84a261292d16f684551e67f2f007199936a.zip
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.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test.lua16
1 files changed, 16 insertions, 0 deletions
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 = {
210 { json.decode, { utf16_escaped }, true, { utf8_raw } } 210 { json.decode, { utf16_escaped }, true, { utf8_raw } }
211} 211}
212 212
213-- The standard Lua interpreter is ANSI C online doesn't support locales
214-- by default. Force a known problematic locale to test strtod()/sprintf().
215local locale_tests = {
216 function ()
217 os.setlocale("cs_CZ")
218 return "Setting locale to cs_CZ (comma separator)"
219 end,
220 { json.encode, { 1.5 }, true, { '1.5' } },
221 { json.decode, { "[ 10, \"test\" ]" }, true, { { 10, "test" } } },
222 function ()
223 os.setlocale("C")
224 return "Reverting locale to POSIX"
225 end
226}
227
213print(string.format("Testing CJSON v%s\n", cjson.version)) 228print(string.format("Testing CJSON v%s\n", cjson.version))
214 229
215run_test_group("decode simple value", decode_simple_tests) 230run_test_group("decode simple value", decode_simple_tests)
@@ -225,6 +240,7 @@ run_test_group("encode table", encode_table_tests)
225run_test_group("decode error", decode_error_tests) 240run_test_group("decode error", decode_error_tests)
226run_test_group("encode error", encode_error_tests) 241run_test_group("encode error", encode_error_tests)
227run_test_group("escape", escape_tests) 242run_test_group("escape", escape_tests)
243run_test_group("locale", locale_tests)
228 244
229cjson.refuse_invalid_numbers(false) 245cjson.refuse_invalid_numbers(false)
230cjson.encode_max_depth(20) 246cjson.encode_max_depth(20)