From 2c8aa83524546e74b0a7f016482aabc55422b742 Mon Sep 17 00:00:00 2001 From: Dylan Fiedler Date: Wed, 23 Oct 2024 09:17:12 -0500 Subject: [PATCH] fix(json): modify to 20 significant digits and check if integer directly Lua 5.3 supports 64 bit integers, which shouldn't need to be converted to strings. Floating point numbers will be rounded to 20 sig digits will be rounded. --- dev-cli/container/src/json.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dev-cli/container/src/json.lua b/dev-cli/container/src/json.lua index 5925a6361..0b55ee8c2 100644 --- a/dev-cli/container/src/json.lua +++ b/dev-cli/container/src/json.lua @@ -72,15 +72,15 @@ end local function encode_number(val) -- Check for NaN, -inf and inf if val ~= val or val <= -math.huge or val >= math.huge then - error("unexpected number value '" .. tostring(val) .. "'") + error("unexpected number value '" .. tostring(val) .. "'") + end + -- Handle integer values separately to avoid floating-point conversion + if math.type(val) == "integer" then + return string.format("%d", val) -- Format as an integer + else + -- Use 20 significant digits for non-integer numbers + return string.format("%.20g", val) end - if math.floor(val) == val then - -- Large integers: avoid scientific notation and print as an integer - return string.format("%.0f", val) - else - -- Decimals: use the 'g' format to print floating point with precision, up to 14 significant digits - return string.format("%.14g", val) - end end local type_func_map = {