# HG changeset patch # User jbe # Date 1452794989 -3600 # Node ID b8477aeb1d9f91380498e25063bb012c9b6ca142 # Parent 4a33becb6f5984595a8a62685cad54af98a74177 json.export(...): avoid exponential notation for integers fitting into lua_Integer represented as float diff -r 4a33becb6f59 -r b8477aeb1d9f libraries/json/json.c --- a/libraries/json/json.c Thu Jan 14 18:52:19 2016 +0100 +++ b/libraries/json/json.c Thu Jan 14 19:09:49 2016 +0100 @@ -3,6 +3,8 @@ #include #include #include +#include +/* TODO: stdint.h only needed for Lua 5.2 compatibility */ // maximum number of nested JSON values (objects and arrays): // NOTE: json_import can store 2^32 / 3 levels on stack swap (using @@ -1120,9 +1122,13 @@ // throw error if number is positive or negative infinity: if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers"); // check if float is integral: - if (trunc(num) == num) { - // avoid exponential notation for (unsigned) 64 bit integers represented as float: - if (fabs(num) < 1e20) { + if ((double)trunc((double)num) == (double)num) { + // avoid exponential notation for integers fitting into lua_Integer represented as float: +#if LUA_VERSION_NUM >= 503 + if (num >= LUA_MININTEGER && num <= LUA_MAXINTEGER) { +#else + if (num >= INT64_MIN && num <= INT64_MAX) { +#endif // use decimal notation: sprintf(numstr, "%.0f", num); } else {