aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-03-03 10:48:09 +1030
committerMark Pulford <mark@kyne.com.au>2012-03-04 20:06:37 +1030
commitd5090bb8f19f4b0fd868a5f9af367ebbb67b7f5a (patch)
treeb963cb47157c384ac8332602e3f0e1e694fc688f
parent4bc5e917c8cd5fc2f6b217512ef530007529322f (diff)
downloadlua-cjson-d5090bb8f19f4b0fd868a5f9af367ebbb67b7f5a.tar.gz
lua-cjson-d5090bb8f19f4b0fd868a5f9af367ebbb67b7f5a.tar.bz2
lua-cjson-d5090bb8f19f4b0fd868a5f9af367ebbb67b7f5a.zip
Use Javascript compat values for Infinity/NaN
Use Javascript compatible values for Infinity/NaN when encoding invalid numbers.
-rw-r--r--dtoa.c4
-rw-r--r--lua_cjson.c16
-rw-r--r--manual.txt9
-rwxr-xr-xtests/test.lua12
4 files changed, 26 insertions, 15 deletions
diff --git a/dtoa.c b/dtoa.c
index 520926c..56398ba 100644
--- a/dtoa.c
+++ b/dtoa.c
@@ -3748,9 +3748,9 @@ dtoa
3748 *decpt = 9999; 3748 *decpt = 9999;
3749#ifdef IEEE_Arith 3749#ifdef IEEE_Arith
3750 if (!word1(&u) && !(word0(&u) & 0xfffff)) 3750 if (!word1(&u) && !(word0(&u) & 0xfffff))
3751 return nrv_alloc("inf", rve, 8); 3751 return nrv_alloc("Infinity", rve, 8);
3752#endif 3752#endif
3753 return nrv_alloc("nan", rve, 3); 3753 return nrv_alloc("NaN", rve, 3);
3754 } 3754 }
3755#endif 3755#endif
3756#ifdef IBM 3756#ifdef IBM
diff --git a/lua_cjson.c b/lua_cjson.c
index ca5b88d..c14a1c5 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -592,12 +592,20 @@ static void json_append_number(lua_State *l, json_config_t *cfg,
592 if (cfg->encode_invalid_numbers == 0) { 592 if (cfg->encode_invalid_numbers == 0) {
593 /* Prevent encoding invalid numbers */ 593 /* Prevent encoding invalid numbers */
594 if (isinf(num) || isnan(num)) 594 if (isinf(num) || isnan(num))
595 json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); 595 json_encode_exception(l, cfg, json, lindex,
596 "must not be NaN or Infinity");
596 } else if (cfg->encode_invalid_numbers == 1) { 597 } else if (cfg->encode_invalid_numbers == 1) {
597 /* Encode invalid numbers, but handle "nan" separately 598 /* Encode NaN/Infinity separately to ensure Javascript compatible
598 * since some platforms may encode as "-nan". */ 599 * values are used. */
599 if (isnan(num)) { 600 if (isnan(num)) {
600 strbuf_append_mem(json, "nan", 3); 601 strbuf_append_mem(json, "NaN", 3);
602 return;
603 }
604 if (isinf(num)) {
605 if (num < 0)
606 strbuf_append_mem(json, "-Infinity", 9);
607 else
608 strbuf_append_mem(json, "Infinity", 8);
601 return; 609 return;
602 } 610 }
603 } else { 611 } else {
diff --git a/manual.txt b/manual.txt
index 3977f1f..a12e378 100644
--- a/manual.txt
+++ b/manual.txt
@@ -290,7 +290,7 @@ Lua CJSON may generate an error when trying to decode numbers not
290supported by the JSON specification. _Invalid numbers_ are defined as: 290supported by the JSON specification. _Invalid numbers_ are defined as:
291 291
292- infinity 292- infinity
293- not-a-number (NaN) 293- NaN
294- hexadecimal 294- hexadecimal
295 295
296Available settings: 296Available settings:
@@ -438,12 +438,13 @@ Lua CJSON may generate an error when encoding floating point numbers not
438supported by the JSON specification (_invalid numbers_): 438supported by the JSON specification (_invalid numbers_):
439 439
440- infinity 440- infinity
441- not-a-number (NaN) 441- NaN
442 442
443Available settings: 443Available settings:
444 444
445+true+:: Allow _invalid numbers_ to be encoded. This will generate 445+true+:: Allow _invalid numbers_ to be encoded using the Javascript
446 non-standard JSON, but this output is supported by some libraries. 446 compatible values +NaN+ and +Infinity+. This will generate
447 non-standard JSON, but these values are supported by some libraries.
447+"null"+:: Encode _invalid numbers_ as a JSON +null+ value. This allows 448+"null"+:: Encode _invalid numbers_ as a JSON +null+ value. This allows
448 infinity and NaN to be encoded into valid JSON. 449 infinity and NaN to be encoded into valid JSON.
449+false+:: Throw an error when attempting to encode _invalid numbers_. 450+false+:: Throw an error when attempting to encode _invalid numbers_.
diff --git a/tests/test.lua b/tests/test.lua
index 96a3201..b8fce84 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -261,10 +261,10 @@ local cjson_tests = {
261 json.encode_invalid_numbers, { false }, true, { false } }, 261 json.encode_invalid_numbers, { false }, true, { false } },
262 { "Encode NaN [throw error]", 262 { "Encode NaN [throw error]",
263 json.encode, { NaN }, 263 json.encode, { NaN },
264 false, { "Cannot serialise number: must not be NaN or Inf" } }, 264 false, { "Cannot serialise number: must not be NaN or Infinity" } },
265 { "Encode Infinity [throw error]", 265 { "Encode Infinity [throw error]",
266 json.encode, { Inf }, 266 json.encode, { Inf },
267 false, { "Cannot serialise number: must not be NaN or Inf" } }, 267 false, { "Cannot serialise number: must not be NaN or Infinity" } },
268 { "Set encode_invalid_numbers(\"null\")", 268 { "Set encode_invalid_numbers(\"null\")",
269 json.encode_invalid_numbers, { "null" }, true, { "null" } }, 269 json.encode_invalid_numbers, { "null" }, true, { "null" } },
270 { "Encode NaN as null", 270 { "Encode NaN as null",
@@ -274,9 +274,11 @@ local cjson_tests = {
274 { "Set encode_invalid_numbers(true)", 274 { "Set encode_invalid_numbers(true)",
275 json.encode_invalid_numbers, { true }, true, { true } }, 275 json.encode_invalid_numbers, { true }, true, { true } },
276 { "Encode NaN", 276 { "Encode NaN",
277 json.encode, { NaN }, true, { "nan" } }, 277 json.encode, { NaN }, true, { "NaN" } },
278 { "Encode Infinity", 278 { "Encode +Infinity",
279 json.encode, { Inf }, true, { "inf" } }, 279 json.encode, { Inf }, true, { "Infinity" } },
280 { "Encode -Infinity",
281 json.encode, { -Inf }, true, { "-Infinity" } },
280 { 'Set encode_invalid_numbers("off")', 282 { 'Set encode_invalid_numbers("off")',
281 json.encode_invalid_numbers, { "off" }, true, { false } }, 283 json.encode_invalid_numbers, { "off" }, true, { false } },
282 284