diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-10-05 23:30:27 +1030 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-10-05 23:30:27 +1030 |
commit | 0f3ab84a261292d16f684551e67f2f007199936a (patch) | |
tree | 5ece4652a08b968232b417f5cf4ceaf234f2be2f /tests | |
parent | 03fa2b508aee4cdf2eac41d53834412ac757feef (diff) | |
download | lua-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-x | tests/test.lua | 16 |
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(). | ||
215 | local 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 | |||
213 | print(string.format("Testing CJSON v%s\n", cjson.version)) | 228 | print(string.format("Testing CJSON v%s\n", cjson.version)) |
214 | 229 | ||
215 | run_test_group("decode simple value", decode_simple_tests) | 230 | run_test_group("decode simple value", decode_simple_tests) |
@@ -225,6 +240,7 @@ run_test_group("encode table", encode_table_tests) | |||
225 | run_test_group("decode error", decode_error_tests) | 240 | run_test_group("decode error", decode_error_tests) |
226 | run_test_group("encode error", encode_error_tests) | 241 | run_test_group("encode error", encode_error_tests) |
227 | run_test_group("escape", escape_tests) | 242 | run_test_group("escape", escape_tests) |
243 | run_test_group("locale", locale_tests) | ||
228 | 244 | ||
229 | cjson.refuse_invalid_numbers(false) | 245 | cjson.refuse_invalid_numbers(false) |
230 | cjson.encode_max_depth(20) | 246 | cjson.encode_max_depth(20) |