Skip to content

Commit 2885270

Browse files
authored
fix(encode_number): support up to 20 significant digits when encoding decmials, return string value of integers
As mentioned in rxi#42 (comment) - Lua 5.3+ supports 64 bit integers. This change modifies the string encoding of numbers to align with those changes.
1 parent dbf4b2d commit 2885270

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

json.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ local function encode_number(val)
108108
if val ~= val or val <= -math.huge or val >= math.huge then
109109
error("unexpected number value '" .. tostring(val) .. "'")
110110
end
111-
return string.format("%.14g", val)
111+
-- Handle integer values separately to avoid floating-point conversion
112+
if math.type(val) == "integer" then
113+
return string.format("%d", val) -- Format as an integer
114+
else
115+
-- Use 20 significant digits for non-integer numbers
116+
return string.format("%.20g", val)
112117
end
113118

114119

0 commit comments

Comments
 (0)