# HG changeset patch # User jbe # Date 1407693876 -7200 # Node ID e61bf25f3d04d5fe4cb337abc18d96222231094a # Parent 2c0db49591be4d17e47decb6d256948c4f72fc59 Dynamic determination of decimal floating point precision in json.export(...) diff -r 2c0db49591be -r e61bf25f3d04 libraries/json/json.c --- a/libraries/json/json.c Sun Aug 10 19:48:09 2014 +0200 +++ b/libraries/json/json.c Sun Aug 10 20:04:36 2014 +0200 @@ -1093,8 +1093,12 @@ if (isnan(num)) return luaL_error(L, "JSON export not possible for NaN value"); // throw error if number is positive or negative infinity: if (isinf(num)) return luaL_error(L, "JSON export not possible for infinite numbers"); - // add Lua's string encoding of the number to the output buffer: - sprintf(numstr, "%.17g", num); + // determine necessary precision to represent double precision floating point number: + sprintf(numstr, "%.14g", num); + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.15g", num); + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.16g", num); + if (strtod(numstr, NULL) != num) sprintf(numstr, "%.17g", num); + // add string encoding of the number to the output buffer: luaL_addstring(&buf, numstr); break; // value to encode is of type boolean: