From 15823a48b31befa5f623134f6d8c57a2f5a603f9 Mon Sep 17 00:00:00 2001 From: aleksanderd Date: Fri, 11 Nov 2022 21:41:19 +0300 Subject: [PATCH] fix #41: Large numbers serialized incorrectly --- json.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/json.lua b/json.lua index 711ef78..2956e16 100644 --- a/json.lua +++ b/json.lua @@ -108,7 +108,12 @@ local function encode_number(val) if val ~= val or val <= -math.huge or val >= math.huge then error("unexpected number value '" .. tostring(val) .. "'") end - return string.format("%.14g", val) + local intVal = math.tointeger(val) + if intVal == val then + return string.format("%d", intVal) + else + return string.format("%.14g", val) + end end