From f79aa68af865ae84b36c7e794beedd87fef2ed54 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 1 Nov 2015 01:51:30 -0700 Subject: feature: now we allow up to 16 decimal places in JSON number encoding via cjson.encode_number_precision(). thanks lordnynex for the patch in #4. Test cases for changing precision --- lua_cjson.c | 2 +- tests/agentzh.t | 15 +++++++++++++++ tests/test.lua | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lua_cjson.c b/lua_cjson.c index 97f2350..5ff3bf7 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -299,7 +299,7 @@ static int json_cfg_encode_number_precision(lua_State *l) { json_config_t *cfg = json_arg_init(l, 1); - return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); + return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 16); } /* Configures how to treat empty table when encode lua table */ diff --git a/tests/agentzh.t b/tests/agentzh.t index e65288f..0b546ff 100644 --- a/tests/agentzh.t +++ b/tests/agentzh.t @@ -50,3 +50,18 @@ print(cjson.encode(b)) --- out ["a=1&b=2"] + + +=== TEST 5: default and max precision +--- lua +local math = require "math" +local cjson = require "cjson" +local double = math.pow(2, 53) +print(cjson.encode(double)) +cjson.encode_number_precision(16) +print(cjson.encode(double)) +print(string.format("%16.0f", cjson.decode("9007199254740992"))) +--- out +9.007199254741e+15 +9007199254740992 +9007199254740992 diff --git a/tests/test.lua b/tests/test.lua index b8fce84..c96dd3d 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -361,7 +361,7 @@ local cjson_tests = { -- Function is listed as '?' due to pcall { "Set encode_number_precision(0) [throw error]", json.encode_number_precision, { 0 }, - false, { "bad argument #1 to '?' (expected integer between 1 and 14)" } }, + false, { "bad argument #1 to '?' (expected integer between 1 and 16)" } }, { "Set encode_number_precision(\"five\") [throw error]", json.encode_number_precision, { "five" }, false, { "bad argument #1 to '?' (number expected, got string)" } }, -- cgit v1.2.3-55-g6feb