diff options
author | Mark Pulford <mark@kyne.com.au> | 2012-03-03 10:48:09 +1030 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2012-03-04 20:06:37 +1030 |
commit | d5090bb8f19f4b0fd868a5f9af367ebbb67b7f5a (patch) | |
tree | b963cb47157c384ac8332602e3f0e1e694fc688f /lua_cjson.c | |
parent | 4bc5e917c8cd5fc2f6b217512ef530007529322f (diff) | |
download | lua-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.
Diffstat (limited to 'lua_cjson.c')
-rw-r--r-- | lua_cjson.c | 16 |
1 files changed, 12 insertions, 4 deletions
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 { |