Skip to content

Commit

Permalink
fix(json): modify json.lua to support larger numbers
Browse files Browse the repository at this point in the history
This is one approach, there are many ways to handle. But given wasm64 and lua 5.3+ support 64 bit integers more precision should be given to raw numbers when encoding.

Ref: permaweb/aos#384
  • Loading branch information
dtfiedler authored Oct 22, 2024
1 parent dbf4b2d commit 8a6eefc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ 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)
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


Expand Down

0 comments on commit 8a6eefc

Please sign in to comment.