webmcp
changeset 432:b8477aeb1d9f
json.export(...): avoid exponential notation for integers fitting into lua_Integer represented as float
author | jbe |
---|---|
date | Thu Jan 14 19:09:49 2016 +0100 (2016-01-14) |
parents | 4a33becb6f59 |
children | 0bbf7717ebfd |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Thu Jan 14 18:52:19 2016 +0100 1.2 +++ b/libraries/json/json.c Thu Jan 14 19:09:49 2016 +0100 1.3 @@ -3,6 +3,8 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <math.h> 1.7 +#include <stdint.h> 1.8 +/* TODO: stdint.h only needed for Lua 5.2 compatibility */ 1.9 1.10 // maximum number of nested JSON values (objects and arrays): 1.11 // NOTE: json_import can store 2^32 / 3 levels on stack swap (using 1.12 @@ -1120,9 +1122,13 @@ 1.13 // throw error if number is positive or negative infinity: 1.14 if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers"); 1.15 // check if float is integral: 1.16 - if (trunc(num) == num) { 1.17 - // avoid exponential notation for (unsigned) 64 bit integers represented as float: 1.18 - if (fabs(num) < 1e20) { 1.19 + if ((double)trunc((double)num) == (double)num) { 1.20 + // avoid exponential notation for integers fitting into lua_Integer represented as float: 1.21 +#if LUA_VERSION_NUM >= 503 1.22 + if (num >= LUA_MININTEGER && num <= LUA_MAXINTEGER) { 1.23 +#else 1.24 + if (num >= INT64_MIN && num <= INT64_MAX) { 1.25 +#endif 1.26 // use decimal notation: 1.27 sprintf(numstr, "%.0f", num); 1.28 } else {